?
Solved

help with text files

Posted on 2003-03-27
9
Medium Priority
?
200 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 53

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 53

Expert Comment

by:Ryan Chong
ID: 8222842
ops,

lineno = lineno +1

should be:

fileno = fileno +1
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 53

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Suggested Courses

770 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