Solved

Split ascii file into parts using vbscript

Posted on 2014-03-31
7
630 Views
Last Modified: 2014-04-08
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
Comment
Question by:crompnk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
7 Comments
 
LVL 45

Expert Comment

by:aikimark
ID: 39966879
Use this regex pattern to capture the letter/name of the group:
.*?"GROUP".*?"([^"])"

Open in new window

0
 

Author Comment

by:crompnk
ID: 39967794
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
 
LVL 45

Expert Comment

by:aikimark
ID: 39967945
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
Industry Leaders: 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!

 

Author Comment

by:crompnk
ID: 39969391
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
 
LVL 45

Assisted Solution

by:aikimark
aikimark earned 250 total points
ID: 39969978
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
 
LVL 55

Expert Comment

by:Bill Prew
ID: 39977781
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
 
LVL 55

Accepted Solution

by:
Bill Prew earned 250 total points
ID: 39978073
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

Industry Leaders: 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!

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

728 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