Solved VBS Script to append 1st line in dir files to an output file

January 3, 2013 at 14:18:19
Specs: Windows Vista, 1.6 gb 4gb

As a newbie I am struggling to write a VBS script and would appreciate some help. I have searched through previous queries but couldn't find anything to match this:

I have directory with a fair number of files in it and wish to write a VBS script which will go through all the files in the directory and copy the first line of each of the files and append each first line to a list in an output file.

I have made a start but am at a loss how to proceed to write the info to the output file

Any help or script would be greatly appreciated.

JuniperGreen

"Script:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("c:\library\")
Set outfile = objFSO.CreateTextFile("c:\library2\Reference.txt")
for each file in folder.Files
Set testfile = objFSO.OpenTextFile(file.path, ForReading)
Do While Not testfile.AtEndOfStream

StrLine = testfile.ReadLine
Loop
Testfile.close
Set testfile = nothing
Next
Outfile.close"


See More: VBS Script to append 1st line in dir files to an output file

Report •


✔ Best Answer
January 4, 2013 at 12:12:20

yes:
(outside loop, set whatever delimiter you want):
delim="::"
(and here's the write statement:)
outfile.writeline strline+delim+file.name

Other attributes of the file are also accessible to the fso: the fiile's times,
size, shortname, etc. Google for and download "script56.chm" if you want a fairly good help file for vbs and javascript.



#1
January 3, 2013 at 17:19:31

This looks like it's getting the last line of each file instead of the first:
Do While Not testfile.AtEndOfStream
StrLine = testfile.ReadLine
Loop
Is that what you want? (My understanding was you wanted the first line of each file.)
If that is the case, you don't want the inner (do-while) loop at all because you are only reading the first line:
for each file in folder.files
(open testfile forreading)
if not testfile.atendofstream then strline=testfile.readline
testfile.close
'and here's the output syntax:
outfile.writeline strline
next
outfile.close
'==== end

Report •

#2
January 4, 2013 at 01:36:04

I dunno vb but here's a bat:

:: ===== script starts here ===============
::
:: juniper.bat 2013-01-04 16:29:27.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b') do (
call :sub1 %%a
)
goto :eof

:sub1
for /f "tokens=* delims= " %%i in (%1) do (
>> newfile echo %%i
goto :done
)
:done
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#3
January 4, 2013 at 07:16:44

Hi Mechanix2Go,

Thank you very much for the batch file. It does exactly what I was after. It's been years since I used batch files (mostly in the old DOS years) so it was educational to see how good old DOS can still be used!

Many thanks


Report •

Related Solutions

#4
January 4, 2013 at 07:46:33

Hi nbrane,

Thank you very much for sorting me out on this one. I have posted the revised version for ease of use for anyone else. It has occurred to me that it would be advantageous if the relevant file name (minus the .txt extension) could be added to the end of the extracted first line of each file. I have tried to achieve this without success. Can this be done or is it not possible in VBS?

Grateful thanks for your help


Script:

"Const ForReading = 1

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("G:\Library\")
Set outfile = objFSO.CreateTextFile("G:\LibraryIndex\Indexout.txt")

For each file in folder.Files

Set testfile = objFSO.OpenTextFile(file.path, ForReading)

If not testfile.atendofstream then strline=testfile.readline

Testfile.close

Set testfile = nothing

Outfile.writeline strline

Next

Outfile.close"


Report •

#5
January 4, 2013 at 12:12:20
✔ Best Answer

yes:
(outside loop, set whatever delimiter you want):
delim="::"
(and here's the write statement:)
outfile.writeline strline+delim+file.name

Other attributes of the file are also accessible to the fso: the fiile's times,
size, shortname, etc. Google for and download "script56.chm" if you want a fairly good help file for vbs and javascript.


Report •

#6
January 5, 2013 at 00:59:40

:: ===== script starts here ===============
::
:: juniper.bat 2013-01-04 16:29:27.10
@echo off > newfile & setLocal enableDELAYedeXpansioN

for /f "tokens=* delims= " %%a in ('dir/b') do (
set N=%%~Na
call :sub1 %%a
)
goto :eof

:sub1
for /f "tokens=* delims= " %%i in (%1) do (
>> newfile echo %%i !N!
goto :done
)
:done
goto :eof
::====== script ends here =================

=====================
M2 Golden-Triangle


Report •

#7
January 5, 2013 at 06:10:42

Untested:
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder("G:\Library\")
Set outfile = fso.CreateTextFile("G:\LibraryIndex\Indexout.txt")

For Each file in folder.Files
  Set testfile = file.OpenAsTextStream
  line = ""
  Do Until line <> "" Or testfile.AtEndOfStream
    line = Trim(testfile.ReadLine)
  Loop
  Outfile.writeline file.Name & ": " & line
Next

How To Ask Questions The Smart Way


Report •

#8
January 17, 2013 at 03:54:25

Razor,

Many thanks for your contribution. It also works a treat! I have amended the output so that the file extension is omitted.

x=(len(file.name)-4)

Outfile.writeline line & " - " & (left(file.name,x))

Grateful thanks


Report •


Ask Question