Solved

Split ascii file into parts using vbscript

Posted on 2014-03-31
7
620 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
  • 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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 53

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 53

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Recently I finished a vbscript that I thought I'd share.  It uses a text file with a list of server names to loop through and get various status reports, then writes them all into an Excel file.  Originally it was put together for our Altiris server…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

813 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now