Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

help with text files

Posted on 2003-03-27
9
Medium Priority
?
201 Views
Last Modified: 2010-05-01
i have large text file which contains a list of data, for example

Field ID     Telephone   Location
------------------------------------------------
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx

Field ID     Telephone   Location
------------------------------------------------
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx

Field ID     Telephone   Location
------------------------------------------------
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx

........and so on,

what i want to do is to split the large file into a number of smaller number of text files
( for instance from the example above in need to split in into 3 text files with the header at the  
  beginning of each file )

Thank you
0
Comment
Question by:apitshah
  • 3
  • 3
  • 2
  • +1
9 Comments
 

Author Comment

by:apitshah
ID: 8222731
i need to build to split a file using visual basic 6.0
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8222839
First use function below to get the file content:

Public Function ReadFileText(ByVal FileName As String) As String
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    Open FileName For Input As #Handle
        ReadFileText = Input$(LOF(Handle), Handle)
        On Error Resume Next
    Close #Handle
    Exit Function
EHandler:
    ShowErrMsg
    On Error Resume Next
    Close #Handle
End Function

then later split the content by using:

Dim tmpArr() As String

tmp = ReadFileText("c:\abc.txt")
tmpArr = Split(tmp, vbcrlf, , vbTextCompare)

From this you can get how many row are available, then do a loop to save it to files:

Public Function WriteFileText(ByVal FileName As String, ByVal Source As String) As String
    On Error GoTo EHandler
    Dim Handle As Integer
    Handle = FreeFile
    Open FileName For Output As #Handle
        Print #Handle, Source
        On Error Resume Next
    Close #Handle
    Exit Function
EHandler:
    ShowErrMsg
    On Error Resume Next
    Close #Handle
End Function

The loop should be like this:

Dim fileno as integer, lineno as integer, v as string
lineno = 20 'Split on each 20 rows
fileno = 1
for i = 0 to ubound(tmparr)
   
     if i mod lineno = 0 then
            WriteFileText "c:\abc" & fileno & ".txt", v
            v = ""
           lineno = lineno +1
     else
           v = v & tmparr(i) & vbcrlf
     end if

next i

if v <> "" then WriteFileText "c:\abc" & fileno & ".txt", v

Hope this helps
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8222842
ops,

lineno = lineno +1

should be:

fileno = fileno +1
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 11

Expert Comment

by:supunr
ID: 8222859

Private Sub SplitFile(FileName As String)
    On Error GoTo SplitErr
    Dim FileNum1 As Integer
    Dim FileNum2 As Integer
    Dim FileSavePath As String
    Dim i As Long
    Dim strLine As String
   
    FileSavePath = "C:\Files\"
    FileNum1 = FreeFile
    Open FileName For Input As #FileNum1
    i = 1
    FileNum2 = FreeFile
    Open FileSavePath & "SplitFile" & i & ".txt" For Output As #FileNum2
    Do While Not EOF(FileNum1)
        Line Input #FileNum1, strLine
        If (Trim(strLine) = "") Then
            Close #FileNum2
            i = i + 1
            FileNum2 = FreeFile
            Open FileSavePath & "SplitFile" & i & ".txt" For Output As #FileNum2
        Else
            Print #FileNum2, strLine
        End If
       
    Loop
    Close #FileNum2
    Close #FileNum1
    Exit Sub
   
SplitErr:
    MsgBox Err.Number & ": " & Err.Description
    Reset ' close any open files
    On Error GoTo 0
End Sub


' Good Luck!
0
 

Author Comment

by:apitshah
ID: 8223309
actually,the file consists of about 4-5 thousand lines, and there is blank line for every 10 data, so what i really want to do is to create a new file just before the next header......for example

Field ID     Telephone   Location
------------------------------------------------
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx

AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
AJI xxxx     xxxxxxxxx   xxxxxxxx
.
.
.
.
.
.

Field ID     Telephone   Location
------------------------------------------------
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx

BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
BBG xxxx     xxxxxxxxx   xxxxxxxx
.
.
.
.
.

Field ID     Telephone   Location
------------------------------------------------
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx

MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
MRC xxxx     xxxxxxxxx   xxxxxxxx
.
.
.
.
.

what i want to do is to create 1 file for "AJI",1 file for "BBG" and 1 file for "MRC", and so on...

I really appreciate the help
0
 
LVL 55

Expert Comment

by:Ryan Chong
ID: 8223327
Just modify the condition so that if empty value is detected then not save it to file, example:

Amendment:

...

for i = 0 to ubound(tmparr)
   
    if trim$(tmparr(i)) <> "" then
       if i mod lineno = 0 then
              WriteFileText "c:\abc" & fileno & ".txt", v
              v = ""
             fileno = fileno +1
             lineno = 0
       else
             v = v & tmparr(i) & vbcrlf
             lineno = lineno +1
       end if
    end if
next i

...
0
 

Expert Comment

by:Tech_Step
ID: 8223366
apitshash

I would use a deliminator (what the Split function uses to actually split the string) of "Field ID" then loop as follows (pseudo code)

private sub splitfile()
    dim filename as string
    array = split(readfiletext(text.txt),"Field ID")
    for i = 0 to Ubound(array)
       array(i) = "Field ID" & array(i)
       filename = "file" & i & ".txt"  
       writefiletext(array(i), filename)
    next i
end sub

where readtextfilecontents and write textfilecontents are the same as the functions described earlier  
0
 
LVL 11

Accepted Solution

by:
supunr earned 120 total points
ID: 8223954
Private Sub SplitFile(FileName As String)
   On Error GoTo SplitErr
   Dim FileNum1 As Integer
   Dim FileNum2 As Integer
   Dim FileSavePath As String
   Dim i As Long
   Dim strLine As String
   
   FileSavePath = "C:\Files\"
   FileNum1 = FreeFile
   Open FileName For Input As #FileNum1
   i = 1
   FileNum2 = FreeFile
   Open FileSavePath & "SplitFile" & i & ".txt" For Output As #FileNum2
   Do While Not EOF(FileNum1)
       Line Input #FileNum1, strLine
       If (Left(Trim(strLine), 5) = "Field") Then
           if (i > 0) then
               Close #FileNum2
               FileNum2 = FreeFile
               Open FileSavePath & "SplitFile" & i & ".txt" For Output As #FileNum2
           End if
           i = i + 1
           Print #FileNum2, strLine
       Else
           Print #FileNum2, strLine
       End If
   Loop
   Close #FileNum2
   Close #FileNum1
   Exit Sub
   
SplitErr:
   MsgBox Err.Number & ": " & Err.Description
   Reset ' close any open files
   On Error GoTo 0
End Sub
0
 

Author Comment

by:apitshah
ID: 8228738
Great....thanks for the help!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

564 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