I have a huge text file that I would like to split into multiple files. The data to put in these multiple files, as well as the filenames for these files is in the source content. Here's a sample of the data that goes on forever:
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
...
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). If it can help, the filenames all start with W, and the content lines all start with a date except the last line that is always $END.
Here's the solution I ended up with in VBScript. Thank you for your help to the ones who contributed.
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