Solved: Append first line of dir files to output file list

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
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"

Vista Home Premium 32 bit
 

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
While I would really prefer to use VBS I can't be choosy as I really need to get this sorted. A batch file version would be appreciated - I haven't used a batch file since the last true DOS in Windows 98. I take it it can still run using CMD in Vista?

Thank you
 

Squashman

Trusted Advisor
Joined
Apr 4, 2003
Messages
19,786
Code:
@echo off
FOR %%G in (c:\library\*) do (
	setlocal enabledelayedexpansion
	set /p line=<"%%~G"
	echo !line!>>"c:\library2\Reference.txt"
	endlocal
)
 
Joined
Jun 8, 2001
Messages
2,583
Here is the VB, Squashmans .bat looks a lot easier!
'Copy First Line of file
' ---------------------------------------------------------------
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

'set up for write file
Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("H:\A1\")
Set outfile = objFSO.CreateTextFile("H:\A2\Reference.txt", ForWriting)
for each file in folder.Files
Set testfile = objFSO.OpenTextFile(file.path, ForReading)
StrLine = testfile.ReadLine
outfile.WriteLine (StrLine)
Testfile.close
Next
Outfile.close
Set testfile = nothing
Set objfso = nothing
 

JuniperGreen

Thread Starter
Joined
Jan 3, 2013
Messages
25
My grateful thanks to Squashman and draceplace for providing me with the batch and vbs solutions. I got both working perfectly!

Cheers guys!
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Members online

Top