Solved

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

Posted on 2015-01-27
8
83 Views
Last Modified: 2015-01-29
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?
0
Comment
Question by:Mike Caldwell
  • 5
  • 3
8 Comments
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40573542
Can you share what you have written so far?

What is the "trigger" criteria?

What data goes into the holding file?

~bp
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40573549
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
Loop

filetxt.Close
Outfile.CLose

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.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40574198
Is this an INI type file by any chance?

Can you provide a sample of the input file?

~bp
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40574692
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
            [EXAMN] => STANFORD, CHRISTOPHER J
            [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.
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 51

Expert Comment

by:Bill Prew
ID: 40576574
Okay, working it.  Do you want to only write out the first match in the file, or every match?

~bp
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 40576645
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
Loop

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

Open in new window

~bp
0
 
LVL 1

Author Comment

by:Mike Caldwell
ID: 40577945
Perfect Bill, thanks.  I can figure out my loops, inner loops, etc, so I've got it from here.  Thanks much.
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 40578841
Very welcome, glad that helped.

~bp
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to dynamically set the form action using jQuery.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now