apitshah
asked on
help with text files
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
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
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
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
ops,
lineno = lineno +1
should be:
fileno = fileno +1
lineno = lineno +1
should be:
fileno = fileno +1
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!
ASKER
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
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
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
...
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
...
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.tx t),"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
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.tx
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Great....thanks for the help!
ASKER