Solved

Split ascii file into parts using vbscript

Posted on 2014-03-31
7
611 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
Comment Utility
Use this regex pattern to capture the letter/name of the group:
.*?"GROUP".*?"([^"])"

Open in new window

0
 

Author Comment

by:crompnk
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:crompnk
Comment Utility
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
Comment Utility
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 51

Expert Comment

by:Bill Prew
Comment Utility
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 51

Accepted Solution

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

15 Experts available now in Live!

Get 1:1 Help Now