Need to read a text file line by line, then write lines with a certain criteria to a save file

I have very large text files with data on a line by line basis.  I need to scan down through the file looking for a keyword that could be a trigger, then write that line out to a holding file.  When the source file reaches EOF, then close it and the target file.

I keep having the open files step on each other, and I'm not sure how to specify reading from one, then writing to the other.  Can I have both open at the same time?
Mike CaldwellConsultant to IP industryAsked:
Who is Participating?
Bill PrewCommented:
Give this a try. Save as a VBS, adjust the filenames, and give a try.

' Define needed constants
Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2

' Define input and output file names
Const Infile = "B:\EE\EE28604950\filein.txt"
Const Outfile = "B:\EE\EE28604950\fileout.txt"

' Define trigger strings to look for
Const Trigger1 = "[APPLICATION]"
Const Trigger2 = "[DOCKN]"

' Create file system object
Set oFSO = CreateObject("Scripting.FileSystemObject")

' Open input and output files
Set oInfile = oFSO.OpenTextFile(Infile, ForReading, False, TriStateUseDefault)
Set oOutfile = oFSO.OpenTextFile(Outfile, ForWriting, True)

' Reset trigger string found flags
bFoundTrigger1 = False
bFoundTrigger2 = False

' Read input file line by line
Do While Not oInfile.AtEndOfStream
   ' Read the next line
   sLine = oInfile.ReadLine

   ' If we have found the first trigger, then look for the second trigger text
   If bFoundTrigger1 And (InStr(1, sLine, Trigger2, vbTextCompare) > 0) Then
      ' Found second trigger, write to output file
      oOutfile.WriteLine sLine
      bFoundTrigger1 = False
      bFoundTrigger2 = True
   End If

   ' If we find the first trigger then set flags to indicate that
   If InStr(1, sLine, Trigger1, vbTextCompare) > 0 Then
      bFoundTrigger1 = True
      bFoundTrigger2 = False
   End If

' Cleanup and end
Set oInfile = Nothing
Set oOutfile = Nothing
Set oFSO = Nothing

Open in new window

Bill PrewCommented:
Can you share what you have written so far?

What is the "trigger" criteria?

What data goes into the holding file?

Mike CaldwellConsultant to IP industryAuthor Commented:
OK, but it is very primative, and won't run due to errors.

dim filesys, filetxt
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Set filesys = CreateObject("Scripting.FileSystemObject")
Set filetxt = filesys.OpenTextFile("G:\API Stuff\Sample Reference List.txt", ForReading, True)
set Outfile = filesys.OpenTextFile("G:\API Stuff\Sample out.txt",ForWriting)

Do until EOF
strLine = filetxt.readLine
Outfile.WriteLine = strLine


Open in new window

The trigger is when "[Application]" is found on a line.  Then I go down a couple more lines and look for another trigger, such as "[FIELD2]", and write the line were FIELD2 was found into the out file.
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

Bill PrewCommented:
Is this an INI type file by any chance?

Can you provide a sample of the input file?

Mike CaldwellConsultant to IP industryAuthor Commented:
stdClass Object
    [APPLICATION] => stdClass Object
            [APNMB] => 12345678
            [PBNUM] => 20090108066
            [PBDAY] => 2009-04-30
            [PANUM] =>
            [PADAY] =>
            [EPDAY] => 2006-06-14
            [FPDAY] => 2006-03-01
            [EXPUN] =>
            [ATYPE] => Utility
            [ASTAT] => Abandoned -- Failure to Respond to an Office Action
            [NSTAT] => App Abandoned
            [STDAY] => 2012-08-27
            [CONFM] => 8142
            [DOCKN] => OP100000929
            [ARTUN] => 2887
            [CLASS] => 235
            [SBCLS] => 472.010

Data like this would be repeated, perhaps hundreds of time.  I just need to first open the file, look for the first trigger, then the second trigger, say DOCKN, then copy out

      [DOCKN] => OP100000929

to the target file.
Bill PrewCommented:
Okay, working it.  Do you want to only write out the first match in the file, or every match?

Mike CaldwellConsultant to IP industryAuthor Commented:
Perfect Bill, thanks.  I can figure out my loops, inner loops, etc, so I've got it from here.  Thanks much.
Bill PrewCommented:
Very welcome, glad that helped.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.