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.AtEndOfStreamStrLine = testfile.ReadLine
Loop
Testfile.close
Set testfile = nothing
Next
Outfile.close"
yes:
(outside loop, set whatever delimiter you want):
delim="::"
(and here's the write statement:)
outfile.writeline strline+delim+file.nameOther 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.
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
I dunno vb but here's a bat: :: ===== script starts here ===============
::
:: juniper.bat 2013-01-04 16:29:27.10
@echo off > newfile & setLocal enableDELAYedeXpansioNfor /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
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
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.FilesSet testfile = objFSO.OpenTextFile(file.path, ForReading)
If not testfile.atendofstream then strline=testfile.readlineTestfile.close
Set testfile = nothing
Outfile.writeline strline
Next
Outfile.close"
yes:
(outside loop, set whatever delimiter you want):
delim="::"
(and here's the write statement:)
outfile.writeline strline+delim+file.nameOther 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.
:: ===== script starts here ===============
::
:: juniper.bat 2013-01-04 16:29:27.10
@echo off > newfile & setLocal enableDELAYedeXpansioNfor /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
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
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
