vbscript to split text file

I need a vbscript to split a file up into different files based on the first two characters in each line. The delimiter characters are: "01", "02", "03", "04" and "05". Only the first two characters in a line are to be considered when determining whether the entire line is a "delimiter line".

Referring to the example file (in code snippet section), the script should:

1. Automatically copy lines 1,2 and 3 to the first output file and then start evaluating the first two characters of each line beginning with line 4. Line 4 and each subsequent line should be copied to the currently open (first) output file UNTIL a line with a delimiter code is reached.

2. As soon as the script encounters a line with a delimiter code, it should copy it, along with the lines that follow, to a new output file (closing the currently open output file) until another line with a delimiter code in positions 1,2 is reached. Repeat this step until EOF.

The script needs to automatically create the necessary output files since the source file will vary in length.

BEFORE]                                    
1HDR-A1095-138-000000-COMBAT  -tA-00000000- 
R1001FAHP34r28W21128403012010055539t003855  
0101012518     P09   42          0000000000 
0901  XS4Z   1b028AB      00100 000000000   
0901  YS4Z   1Z490AA      00100 000000000   
10011000F      000015       013000011121>   
10011000AXQ    000015       002000001803>   
1301CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1201TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
050201253109B11              >              
0901   MTt   45900C       00100 000000000   
10021000A11    000015       003000002105>   
10011000A13    000015       008000001213>   
1302CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1202TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
0802YP1111                              P01 
020301253109B11              >              
0903  XS4Z   1H148AA      00100 000000000   
0903  XS4Z   1C155AB      00200 000000000   
10031000A11    000015       003000002105>   
10031000A13    000015       008000001213>   
1303CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1203TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
0803YP1111                              P01 
1EOF-bW03111055-00033-         -138-        
                                           
AFTER]                                     
Part1.txt]                                 
1HDR-A1095-138-000000-COMBAT  -tA-00000000- 
R1001FAHP34r28W21128403012010055539t003855  
0101012518     P09   42          0000000000 
0901  XS4Z   1b028AB      00100 000000000   
0901  YS4Z   1Z490AA      00100 000000000   
10011000F      000015       013000011121>   
10011000AXQ    000015       002000001803>   
1301CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1201TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
                                           
Part2.txt]                                 
050201253109B11              >              
0901   MTt   45900C       00100 000000000   
10021000A11    000015       003000002105>   
10011000A13    000015       008000001213>   
1302CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1202TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
0802YP1111                              P01 
                                           
Part3.txt]                                 
020301253109B11              >              
0903  XS4Z   1H148AA      00100 000000000   
0903  XS4Z   1C155AB      00200 000000000   
10031000A11    000015       003000002105>   
10031000A13    000015       008000001213>   
1303CHaCt arGIra LIGHT I.b.S. TaST Arb bIAG 
1203TRArS bIAG-CObaS P0134-P0151,ROAb TaST- 
0803YP1111                              P01 
1EOF-bW03111055-00033-         -138-

Open in new window

Muskie12Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

exx1976Commented:
No problem. Give me 5 minutes.
0
exx1976Commented:
This should do it, just change the input and output paths/names to fit your needs.  The output files will be numbered starting with a 0 and incrementing by 1 each time.


HTH,
exx
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oSource = oFS.OpenTextFile("c:\mysourcefile.txt")
i = 0
Targetfilename = "c:\target" & i & ".txt"
Set oTarget = oFS.CreateTextFile(Targetfilename)
For q = 0 To 3
	data = oSource.ReadLine
	oTarget.WriteLine
Next
Do While Not oSource.AtEndOfStream
	data = oSource.ReadLine
	If Left(data,2) = "01" Or Left(data,2) = "02" Or Left(data,2) = "03" Or Left(data,2) = "04" Or Left(data,2) = "05" Then
		oTarget.Close
		i = i + 1
		Targetfilename = "c:\target" & i & ".txt"
		Set oTarget = oFS.CreateTextFile(Targetfilename)
	End If
	oTarget.WriteLine(data)
Loop
oTarget.Close
oSource.Close

Open in new window

0
Muskie12Author Commented:
close - when testing with the test file content I uploaded, output files 2 and 3 are good but the first file is actually missing the first 4 lines.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

exx1976Commented:
oops.

This one should do it.


-exx
Set oFS = CreateObject("Scripting.FileSystemObject") 
Set oSource = oFS.OpenTextFile("c:\mysourcefile.txt") 
i = 0 
Targetfilename = "c:\target" & i & ".txt" 
Set oTarget = oFS.CreateTextFile(Targetfilename) 
For q = 0 To 3 
        data = oSource.ReadLine 
        oTarget.WriteLine(data)
Next 
Do While Not oSource.AtEndOfStream 
        data = oSource.ReadLine 
        If Left(data,2) = "01" Or Left(data,2) = "02" Or Left(data,2) = "03" Or Left(data,2) = "04" Or Left(data,2) = "05" Then 
                oTarget.Close 
                i = i + 1 
                Targetfilename = "c:\target" & i & ".txt" 
                Set oTarget = oFS.CreateTextFile(Targetfilename) 
        End If 
        oTarget.WriteLine(data) 
Loop 
oTarget.Close 
oSource.Close

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Muskie12Author Commented:
Thanks buckets exx!
0
exx1976Commented:
Any time, thank you for the points!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.