1
votes

I have this sample of text file:

W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
W1M0200
03/12/2012 00:30 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_002 12 11 136 58 0 0 0 1
03/12/2012 00:30 SS_003 3 2 213 91 0 0 0 1
03/12/2012 00:30 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:30 SS_008 0 0 0 0 0 0 0 0
$END
W1M0230
...

and this is my vbscript code:

textFile = "C:\data.txt"
saveTo = "C:\"
writeTo = ""
headingPattern = "(W[0-9][A-Z][0-9]*)"

Dim fso, fileFrom, regex
Set fso = CreateObject("Scripting.FileSystemObject")
Set fileFrom = fso.OpenTextFile(textFile)
Set regex = New RegExp

With regex
  .Pattern = headingPattern
  .IgnoreCase = false
  .Global = True
End With

While fileFrom.AtEndOfStream <> True
  line = fileFrom.ReadLine
  Set matches = regex.Execute(line)

  If matches.Count > 0 Then
    writeTo = saveTo & matches(0).SubMatches(0) & ".txt"
    Set fileTo = fso.CreateTextFile(writeTo)
  Else
    fileTo.WriteLine(line)
  End If
Wend

Set fileFrom = Nothing
Set fso = Nothing
Set regex = Nothing

The filename of the first output file would be W1M0130.txt and the content would be the lines below, down to the next filename (W1M0200). The filenames all start with W, and the content lines all start with a date except the last line that is always $END.

The VBScript code works fine ONLY if the pattern in the text file is only one (W1M0130 then W1M0200 then W1M0230) but I am having a hard time if the text file contains two or more of that pattern. (W1M0130 then another W1M0130 then W1M0200 then W1M0230)

W1M0130.txt should be like:

W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
W1M0130
03/12/2012 00:00 SS_001 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_002 15 14 149 64 0 0 0 1
03/12/2012 00:00 SS_003 4 3 233 100 0 0 0 1
03/12/2012 00:00 SS_004 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_005 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_006 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_007 0 0 0 0 0 0 0 0
03/12/2012 00:00 SS_008 0 0 0 0 0 0 0 0
$END
1

1 Answers

0
votes

CreateTextFile replaces existing files, so you're replacing what's been written before. Use OpenTextFile to open the file for appending. Set the third parameter to True to create the file if it doesn't exist.

Set fileTo = fso.OpenTextFile(writeTo, 8, True)

Also, close the file when you meet a line $END:

Else
  fileTo.WriteLine(line)
  If line = "$END" Then fileTo.Close
End If

Side note: setting objects to Nothing is pointless most of the time, so don't do it unless you have a reason.