Breaking Text Files
Breaking Text Files
(OP)
I've been trying to do this for the past few days and cannot get it. All I want to do is break and initial text file that has a whole bunch of information in two files that contain only the lines that start with ND in one file, and the other file with all the lines that start with EL.
a Sample text file would be something like this...
flajfl
afalfj
ND, 3 4 5
ND, 3 6 7
EL, 1 2 3
EL 123
I was trying to use the split function, but I got all confused with it...This is what I have so far...I am looking for the letter "H" as the start of the line in this code...
Sub Perm()
Dim variable1 As String
Dim StartTag As String
Dim TextIn As String
Dim EndTag As String
Dim lArray As Variant
StartTag = "H"
Wrap$ = Chr$(13) + Chr$(10)
EndTag = Wrap$
Open "C:\Documents and Settings\Javier B\Desktop\sample.txt" For Input As #1
Do Until EOF(1)
Input #1, variable1
Alltext$ = Alltext$ & variable1 & Wrap$
Loop
charsInFile = Len(Alltext$)
For i = 1 To charsInFile
letters = Mid(Alltext, i, 1)
If letters = Chr(13) Then
j = i + 1
sentence = Mid(Alltext, j)
'Debug.Print sentence
TextIn = sentence
Extract = " "
lArray = Split(TextIn, StartTag)
'If IsArray(lArray) Then
Extract = lArray(0)
Debug.Print lArray(0)
'lArray = Split(Extract, EndTag)
'If IsArray(lArray) Then
'Extract = lArray(0)
'Else
'Extract = " "
'End If
'End If
'Debug.Print Extract
End If
Next i
Close #1
End Sub
Any help or guidance would be greatly appreciated...
Thank you,
Javier
a Sample text file would be something like this...
flajfl
afalfj
ND, 3 4 5
ND, 3 6 7
EL, 1 2 3
EL 123
I was trying to use the split function, but I got all confused with it...This is what I have so far...I am looking for the letter "H" as the start of the line in this code...
Sub Perm()
Dim variable1 As String
Dim StartTag As String
Dim TextIn As String
Dim EndTag As String
Dim lArray As Variant
StartTag = "H"
Wrap$ = Chr$(13) + Chr$(10)
EndTag = Wrap$
Open "C:\Documents and Settings\Javier B\Desktop\sample.txt" For Input As #1
Do Until EOF(1)
Input #1, variable1
Alltext$ = Alltext$ & variable1 & Wrap$
Loop
charsInFile = Len(Alltext$)
For i = 1 To charsInFile
letters = Mid(Alltext, i, 1)
If letters = Chr(13) Then
j = i + 1
sentence = Mid(Alltext, j)
'Debug.Print sentence
TextIn = sentence
Extract = " "
lArray = Split(TextIn, StartTag)
'If IsArray(lArray) Then
Extract = lArray(0)
Debug.Print lArray(0)
'lArray = Split(Extract, EndTag)
'If IsArray(lArray) Then
'Extract = lArray(0)
'Else
'Extract = " "
'End If
'End If
'Debug.Print Extract
End If
Next i
Close #1
End Sub
Any help or guidance would be greatly appreciated...
Thank you,
Javier
RE: Breaking Text Files
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
RE: Breaking Text Files
Good Luck
johnwm
________________________________________________________
To get the best from these forums read FAQ731-376 before posting
Steam Engine enthusiasts: www.essexsteam.co.uk
RE: Breaking Text Files
Another solution would be to take a look at regular expressions in VB. You can use the regular expression matches to filter out the contents of the initial file.
You may also want to take a look at the FileSystemObject for reading/writing files. It always seems to be much easier than the Open function in my opinion.
RE: Breaking Text Files
1) open file 0 to read from
2) open file 1 to write to
3) open file 2 to write to
read line from file 0
if the two left characters = "ND" then write line to file 1
if the two left characters = "EL" then write line to file 2
Loop for all lines in file 0
You then have file 1 containing just lines starting ND, and file 2 just containing lines starting EL
All the necessary code is in the VB help files.
Hope this is what you want.
RE: Breaking Text Files
My biggest problem is how to do the if statements that you are proposing, I cannot find the function to read the two left characters...I'd really appreciate any thoughts...
Thank you in advanced
RE: Breaking Text Files
Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
RE: Breaking Text Files
However, right now I have it printing to the inmediate window and is looking for lines that start with "Ho"...
Thank to you all
Sub Breaking()
Open "C:\Documents and Settings\Javier B\Desktop\sample.txt" For Input As #1
Do Until EOF(1)
Line Input #1, variable1
Dim CheckFor As String
CheckFor = "Ho"
lLen = Len(CheckFor)
sCompare = Left(variable1, lLen)
StringStartsWith = StrComp(sCompare, CheckFor, vbTextCompare)
If StringStartsWith = 0 Then
Debug.Print variable1
End If
Loop
Close #1
End Sub
RE: Breaking Text Files
If StrComp(Left(Variable1, 2), "ND") = 0 Then 'we have a match
print into file1
ElseIf StrComp(Left(Variable1, 2), "EL") = 0 Then 'we have a different match
print into file2
EndIf
Note, I would ALWAYS use the FreeFile function to get a file number as it can save a lot of problems.
RE: Breaking Text Files
CODE
Dim sLine As String
Open "C:\Output.txt" For Output As #0
Open "C:\ND.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, sLine
If (UCase$(Left$(sLine, 2)) = "ND") Then
Print #0, sLine
End If
Loop
Close #1
Open "C:\EL.txt" For Input As #2
Do While Not EOF(2)
Line Input #2, sLine
If (UCase$(Left$(sLine, 2)) = "EL") Then
Print #0, sLine
End If
Loop
Close #0
Close #2
End Sub
-HTH,
Nick
RE: Breaking Text Files
"use the FreeFile function to get a file number as it can save a lot of problems."