jchionglou
asked on
HOW TO SPLIT A TEXT FILE INTO SEVERAL INDEPENDENT FILES ACCORDING TO SOME FLAGS FOUND IN THE ORIGINAL FILE
I have to read a text file, for which I don't know the size. All I know is that I should separate it into as many files as information it contains. The text files looks like this:
ISA/00/ /00/ /ZZ/PAJCANADA /01/200091189 /060620/0841/U/00401/00010 0536/0/P/> GS/FA/PAJ CANADA/200 091189/200 60620/0840 /100536001 /X/004010V ICSST/997 /0001AK1/ RA/3040025 29AK2/820 /304003152 AK5/AAK9 /A/1/1/1S E/6/0001G E/1/100536 001IEA/1/ 000100536 ISA/00/ /00/ /ZZ/PAJCANADA /01/202377222 /060620/0841/U/00401/00010 0457/0/P/> GS/FA/PAJ CANADA/202 377222/200 60620/0840 /100457001 /X/004010V ICSST/997 /0002AK1/ RA/3040506 22AK2/820 /304051097 AK5/AAK9 /A/1/1/1S E/6/0002G E/1/100457 001IEA/1/ 000100457 ISA*00* *00* *ZZ*PAJCANADA *12*4165106466 AAK9*A*1*1*1SE*6*0003GE *1*1004640 01IEA*1*0 00100464I SA*00* *00* *ZZ*PAJCANADA *12*4169413922 00550AK2*850*001001294AK 5*AAK9*A* 1*1*1SE*6 *0004GE*1 *100767001 IEA*1*000 100767
The independen Files are separated by the string "ISA" . In the sample file, I should divide it into four independent text files. Each starting with the String "ISA"
The routine should stop once it has separated the original file.
ISA/00/ /00/ /ZZ/PAJCANADA /01/200091189 /060620/0841/U/00401/00010
The independen Files are separated by the string "ISA" . In the sample file, I should divide it into four independent text files. Each starting with the String "ISA"
The routine should stop once it has separated the original file.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks mvidas. Your solution works perfectly.
Glad to help! Normally I probably would have used Chr(0) instead of Chr(255) but wasn't sure if the boxes in your above code were 0 or not. Let me know if you need anything else.
Matt
Matt
Option Explicit
Private Sub Form_Load()
Dim i As Long
Dim j As Long
Dim vSplit() As String
Dim vSplitFiles() As String
Dim strContents As String
Dim objFSO
Dim objFile
Set objFSO = CreateObject("Scripting.Fi
Set objFile = objFSO.OpenTextFile(App.Pa
strContents = objFile.ReadAll
objFile.Close
'Debug.Print strContents
vSplitFiles = Split(strContents, "ISA")
For j = 0 To UBound(vSplitFiles)
Debug.Print "ISA", j
vSplit = Split(strContents, "/")
'Data after 6th /
Debug.Print "Data after 6th /", vSplit(6)
'Data after first X
For i = 0 To UBound(vSplit)
'Debug.Print i, vSplit(i)
If vSplit(i) = "X" Then
Debug.Print "Data after first X", vSplit(i + 1)
End If
Next
Next
End Sub