Solved

HOW TO SPLIT A TEXT FILE INTO SEVERAL INDEPENDENT FILES ACCORDING TO SOME FLAGS FOUND IN THE ORIGINAL FILE

Posted on 2006-06-22
4
337 Views
Last Modified: 2008-01-09
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/000100536/0/P/>GS/FA/PAJCANADA/200091189/20060620/0840/100536001/X/004010VICSST/997/0001AK1/RA/304002529AK2/820/304003152AK5/AAK9/A/1/1/1SE/6/0001GE/1/100536001IEA/1/000100536ISA/00/          /00/          /ZZ/PAJCANADA      /01/202377222      /060620/0841/U/00401/000100457/0/P/>GS/FA/PAJCANADA/202377222/20060620/0840/100457001/X/004010VICSST/997/0002AK1/RA/304050622AK2/820/304051097AK5/AAK9/A/1/1/1SE/6/0002GE/1/100457001IEA/1/000100457ISA*00*          *00*          *ZZ*PAJCANADA      *12*4165106466 AAK9*A*1*1*1SE*6*0003GE*1*100464001IEA*1*000100464ISA*00*          *00*          *ZZ*PAJCANADA      *12*4169413922     00550AK2*850*001001294AK5*AAK9*A*1*1*1SE*6*0004GE*1*100767001IEA*1*000100767



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.
0
Comment
Question by:jchionglou
  • 2
4 Comments
 
LVL 35

Accepted Solution

by:
mvidas earned 500 total points
ID: 16962591
Hello,

Give the following a try, should be what you need:

Sub jchionglou()
 Dim vFF As Long, vFile As String, vFileExt As String
 Dim tempStr As String, tempArr() As String, i As Long
 vFile = "C:\txtfile"
 vFileExt = ".txt"
 vFF = FreeFile
 Open vFile & vFileExt For Binary As #vFF
 tempStr = Space$(LOF(vFF))
 Get #vFF, , tempStr
 Close #vFF
 tempStr = Replace(tempStr, "ISA", Chr(255) & "ISA")
 tempArr = Split(tempStr, Chr(255))
 For i = 1 To UBound(tempArr)
  vFF = FreeFile
  Open vFile & "-" & CStr(i) & vFileExt For Output As #vFF
  Print #vFF, tempArr(i);
  Close #vFF
 Next
End Sub

Matt
0
 
LVL 22

Expert Comment

by:danaseaman
ID: 16962788
Try this:

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.FileSystemObject")
   Set objFile = objFSO.OpenTextFile(App.Path & "\test.txt", 1)
   
   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
0
 

Author Comment

by:jchionglou
ID: 16986080
Thanks mvidas.  Your solution works perfectly.
0
 
LVL 35

Expert Comment

by:mvidas
ID: 16986124
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
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

Suggested Solutions

Title # Comments Views Activity
MS Access - Capture pressed key onclick 4 28
Getting warning: You are about to delete 1 row(s) 9 43
Add and format columns in vb6 7 46
Excel Automation VBA 19 30
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

911 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

26 Experts available now in Live!

Get 1:1 Help Now