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
86 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 54

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 54

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
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 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
 
LVL 54

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 54

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 54

Expert Comment

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

~bp
0

Featured Post

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
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…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

685 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