×
INTELLIGENT WORK FORUMS
FOR ENGINEERING PROFESSIONALS

Log In

Come Join Us!

Are you an
Engineering professional?
Join Eng-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Eng-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Breaking Text Files
2

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

RE: Breaking Text Files

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

RE: Breaking Text Files

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: www.essexsteam.co.uk

RE: Breaking Text Files

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.

RE: Breaking Text Files

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.

RE: Breaking Text Files

(OP)
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

RE: Breaking Text Files

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

RE: Breaking Text Files

(OP)
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

RE: Breaking Text Files

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.

RE: Breaking Text Files

+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

RE: Breaking Text Files

Please

"use the FreeFile function to get a file number as it can save a lot of problems."

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Eng-Tips Forums free from inappropriate posts.
The Eng-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Eng-Tips forums is a member-only feature.

Click Here to join Eng-Tips and talk with other members! Already a Member? Login



News


Close Box

Join Eng-Tips® Today!

Join your peers on the Internet's largest technical engineering professional community.
It's easy to join and it's free.

Here's Why Members Love Eng-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close