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
84 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 52

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 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 52

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 52

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 52

Expert Comment

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

~bp
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at http://www.experts-exchange.com/ARTH_5123186.html (http://www.experts-ex…
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 …
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

919 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

20 Experts available now in Live!

Get 1:1 Help Now