• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 641
  • Last Modified:

Split ascii file into parts using vbscript

Hi,
I have an ascii text file that I am trying to split out text in the file based on a Header. The header is the "GROUP", so a new file would be created for each "GROUP" i.e. GROUP A, GROUP B, GROUP C.....
The new file would just contain the rows below each GROUP section.

An example file looks like this:

"GROUP","A"
"DATA","ID01","X","X","X","X","X"
"DATA","ID02","X","X","X","X","X"
"DATA","ID03","X","X","X","X","X"

"GROUP","B"
"DATA","ID20","X","X","X","X","X"
"DATA","ID21","X","X","X","X","X"
"DATA","ID22","X","X","X","X","X"

"GROUP","C"
"DATA","ID30","X","X","X","X","X"
"DATA","ID31","X","X","X","X","X"
"DATA","ID32","X","X","X","X","X"

"GROUP","D"
"DATA","ID40","X","X","X","X","X"
"DATA","ID41","X","X","X","X","X"
"DATA","ID42","X","X","X","X","X"

Using the example code below will split the first group, how do I write the headingPattern and modify the code to read each header, currently the script just wants to overwrite the first file created?

textFile = "C:\temp\04test.txt"
saveTo = "C:\temp\"
writeTo = ""
headingPattern = "(GROUP*)"

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

Open in new window


Thank you
Examplefile.txt
0
crompnk
Asked:
crompnk
  • 3
  • 2
  • 2
2 Solutions
 
aikimarkCommented:
Use this regex pattern to capture the letter/name of the group:
.*?"GROUP".*?"([^"])"

Open in new window

0
 
crompnkAuthor Commented:
Hi, thanks for the reply, is this scripted as:

headingPattern = ".*?"GROUP".*?"([^"])"

Open in new window


This gave an 'expected end of statement error' in the vbscript.
0
 
aikimarkCommented:
When a string literal contains a quote character, you need to either double up the interior quote characters or concatenate chr(34) characters.
headingPattern = ".*?""GROUP".*?""([^""])"

Open in new window

or
headingPattern = ".*?" & Chr(34) & "GROUP".*?" & Chr(34) & "([^" & Chr(34) & "])"

Open in new window

0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
crompnkAuthor Commented:
Hi,
Using the above script with the headingPattern changed I get an 'Expected identifier' error. I tried taking out the '*' as this is the position of the error:

Script:    C:\temp\VBScript.vbs
Line:      4
Char:      31
Error:     Expected identifier
Code:     800A03F2
Source   Microsoft VBScript compilation error

Attached is the VBScript file and the text file being called.

Thanks
VBScript.vbs
04test.txt
0
 
aikimarkCommented:
I missed one of the interior quotes.  My earlier snippets should have been
headingPattern = ".*?""GROUP"".*?""([^""])"

Open in new window

and
headingPattern = ".*?" & Chr(34) & "GROUP" & Chr(34) & ".*?" & Chr(34) & "([^" & Chr(34) & "])"

Open in new window

0
 
Bill PrewCommented:
Looks like you are in good hands here, but I would also recommend doing a fileTo.Close before you start the next group (only if fileTo is not Nothing) just to ensure the prior group file is written and closed properly.

~bp
0
 
Bill PrewCommented:
This seems to work for me:

textFile = "C:\temp\04test.txt"
saveTo = "C:\temp\"
writeTo = ""
headingPattern = """(GROUP)"",""(.*)"""

set fso = CreateObject("Scripting.FileSystemObject")
set fileFrom = fso.OpenTextFile(textFile)
set regex = new RegExp
set fileTo = nothing

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) & " " & matches(0).SubMatches(1) & ".txt"
    if not (fileTo is nothing) then fileTo.Close()
    set fileTo = fso.CreateTextFile(writeTo)
  else
    fileTo.WriteLine(line)
  end if
wend

fileFrom.Close()

set fileFrom = nothing
set fso = nothing
set regex = nothing

Open in new window

~bp
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now