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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reading Lines from Multiple Text files

Status
Not open for further replies.

babaki

Technical User
Feb 7, 2003
5
0
0
US
OK im kind of stuck here..
I have 200 text files that look similiar to this, all with different file names:

The Student ID is EAS
The Student SSN is 121788386

I need to write a script that will take only the ID and the SSN # from each file and write it to a single log in the following format:

EAS,121788386
EEN,123456789
etc...

Thanks in advnace
 
Try this:

Use the GetFolder method on the folder where the files exist.

Loop through the returned GetFolder object with a "For Each... In... Next" loop and pull out the filenames from the Files property of the returned object the GetFolder created.

Stick these into your final log file in the format you want. Use FileSystemObject methods and properties to accomplish most of the above.

This will help you get the general idea of how to accomplish the task. It'll be up to you to learn the coding syntax unless someone else is willing to code it up for you. The Windows Script Technologies help file is invaluable when you start working with VBS/WSH.

 
Ok this is what I have figured out so far, with some help from another post. It reads 2 lines from the text file, splits it into ana rray, then writes the parts of the array that i want to another text file. My problem is that I need to run this on a whole bunch of text files in a folder, not just one that I specify in the script.
Thanks

Const ForReading = 1
Dim fso, MyFile, MyFile2, Text, Fail, myline, linewewant

Set fso = CreateObject("Scripting.FileSystemObject")
Set MyFile = fso_OpenTextFile("c:\eab.txt", ForReading)

Do While myFile.AtEndOfStream <> True

teststr = myfile.readline
if instr(teststr, &quot;Branch&quot;) > 0 then exit do

loop

Do While myfile.AtEndofStream <> True

teststr2 = myfile.readline
if instr(teststr2, &quot;StorageCard&quot;) > 0 then exit do

loop

myfile.close


linewewant=split(teststr, &quot; &quot;)
linewewant2=split(teststr2, &quot; &quot;)
myline = linewewant(3)
myline2 = linewewant2(4)

Set MyFile2 = fso.CreateTextFile(&quot;c:\log.txt&quot;)
MyFile2.write (myline)
MyFile2.write (&quot;,&quot;)
MyFile2.writeline (myline2)
myfile2.close
 
if you c:\>dir *.txt /b >> files.log

you'll get a txt file with the names of all your files.

then you can just do what you're already doing nested

so
Do While logfile.AtEndOfStream <> True

 filename = myfile.readline
Do While filename.AtEndOfStream <> True

i think you could use one loop to read the file too, although it would read the whole file.

Do While myFile.AtEndOfStream <> True
  teststr = myfile.readline
  if instr(teststr, &quot;Branch&quot;) > 0 then line1=teststr end if
if instr(teststr2, &quot;StorageCard&quot;) > 0 then line2=teststr end if
loop

just a bit less code.
then
if the id is always 3 letters u could d0
myline = right(line1,3)

also you could do

MyFile2.write (myline & &quot;,&quot; & myline2)


hope this helps ===============
Security Forums
 
Thanks for all your help,i took some ofyour advice..heres what I came up with and it works. Instead of doing the dir /b, it loops for each file until it has gone through all of them in the folder. Thanks again, I learned ALOT of basics ,considering I have never used VBS before


Const ForReading = 1
Dim fso, MyFile, MyFile2, Text, Fail, f, fc, f1

Set fso = CreateObject(&quot;Scripting.FileSystemObject&quot;)
Set f = fso.GetFolder(&quot;c:\Test&quot;)
Set fc = f.Files
Set MyFile2 = fso.CreateTextFile(&quot;c:\log.txt&quot;)
Myfile2.close

For Each f1 in fc

Set MyFile = fso_OpenTextFile(&quot;c:\test\&quot; & f1.name)

Do While myFile.AtEndOfStream <> True
teststr = myfile.readline
if instr(teststr, &quot;Branch&quot;) > 0 then exit do
loop

Do While myfile.AtEndofStream <> True
teststr2 = myfile.readline
if instr(teststr2, &quot;StorageCard&quot;) > 0 then exit do
loop

myfile.close

linewewant=split(teststr, &quot; &quot;)
linewewant2=split(teststr2, &quot; &quot;)
myline = linewewant(3)
myline2 = linewewant2(4)

set myfile2 = fso.opentextfile(&quot;c:\log.txt&quot;, 8)
MyFile2.write (myline)
MyFile2.write (&quot;,&quot;)
MyFile2.writeline (myline2)
myfile2.close

Next
msgbox (&quot;done&quot;)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top