• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 776
  • Last Modified:

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

0
Muskie12
Asked:
Muskie12
  • 4
  • 2
1 Solution
 
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 expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

 
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
 
Muskie12Author Commented:
Thanks buckets exx!
0
 
exx1976Commented:
Any time, thank you for the points!
0

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now