Solved

Split ascii file into parts using vbscript

Posted on 2014-03-31
7
614 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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 52

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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 Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

24 Experts available now in Live!

Get 1:1 Help Now