Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Breaking Text Files 2

Status
Not open for further replies.

ljavier34

Mechanical
Dec 2, 2005
13
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
 
Replies continue below

Recommended for you

I would process the file one line at a time. Instead of using Input, use Line Input and do everything inside the loop.

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
You may also note that there is an intrinsic VB constant for Chr(13) & Chr(10). You can simply use vbCRLF for the combination.

Good Luck
johnwm
________________________________________________________
To get the best from these forums read faq731-376 before posting

Steam Engine enthusiasts:
 
This type of problem can be solved quite easily using awk, but unfortunately, awk is not available as standard on Windows. If you can download and install it on your machine, it might make the problem easier to solve.

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.
 
If I understand what you want then my approach would be :-

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.
 
Peter,

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
 
Have you looked at the LEFT function?

Good Luck
--------------
As a circle of light increases so does the circumference of darkness around it. - Albert Einstein
 
I finally got the program to do what I wanted... The problem ended up being the loop which was misplaced...Here's the code if someone else ever needs it..
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
 
Working from my example I would have used --

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.
 
+1 for Peter's suggestions. You might try something like the following:

Code:
Sub Breaking()

    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
 
Please

"use the FreeFile function to get a file number as it can save a lot of problems."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor