Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read multiple files and write output to a single file

Status
Not open for further replies.

skelly9131

Programmer
Feb 21, 2003
3
US
Hi all, I'm teaching myself VBScript and have been able to get a script working that can read a single file and write certain records to an output file (code shown below).

What I can't figure how to do is modifiy this code to read multiple files in the same directory and write/append certain records to the same output file.

I've tried using the Files Collection Object, but I'm just not getting it yet. I've seen examples of scripts using things such as File.Name, but the book I'm using doesn't list all the properties available. Is there a master list of properties and their uses? Any help would be greatly appreciated.


Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("c:\test\")
Set testfile = objFSO.OpenTextFile("c:\test\test.txt", ForReading)
Set outfile = objFSO.CreateTextFile("c:\test\testout.txt")

for each file in folder.Files

Do While Not testfile.AtEndOfStream
line = testfile.readline
linetype = left(line,4)
If linetype = "856A" Then
customer = mid(line,80,5)
End If
If customer = "2226 " or customer = "2227 " Then
outfile.writeline(line)
End If
Loop

MsgBox linetype
MsgBox line
MsgBox customer

next

testfile.close
outfile.close
 
[tt]Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("c:\test\")
[red]'[/red]Set testfile = objFSO.OpenTextFile("c:\test\test.txt", ForReading) [red]'commented[/red]
Set outfile = objFSO.CreateTextFile("c:\test\testout.txt")

for each file in folder.Files

[blue]if lcase(objFSO.getExtensionName(file.path))="txt" then[/blue]

[blue]Set testfile = objFSO.OpenTextFile(file.path, ForReading)[/blue]
Do While Not testfile.AtEndOfStream
line = testfile.readline
linetype = left(line,4)
If linetype = "856A" Then
customer = mid(line,80,5)
End If
If customer = "2226 " or customer = "2227 " Then
outfile.writeline(line)
End If
Loop
MsgBox linetype
MsgBox line
MsgBox customer

[blue]testfile.close[/blue]

[blue]end if[/blue]

next

[red]'[/red]testfile.close [red]'commented[/red]
outfile.close
[/tt]
 
tsuji, thanks for your post. You got me on the right path. Your script was causing looping because the output file was being written to the same directory. So, here's what worked for me.


Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set folder = objFSO.GetFolder("c:\test\")
Set outfile = objFSO.CreateTextFile("c:\test2\testout.txt")

for each file in folder.Files

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

Do While Not testfile.AtEndOfStream
line = testfile.readline
linetype = left(line,4)
If linetype = "856A" Then
customer = mid(line,80,5)
End If
If customer = "6001 " Then
outfile.writeline(line)
End If
Loop

testfile.close

next

outfile.close
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top