Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

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

Go to next line in text file

Status
Not open for further replies.

denko

Programmer
Joined
May 17, 2001
Messages
70
Location
US
I've got a text file where if criteria is not true i need to read next line

text file:
David Toms
John Doe
John Smith
Kenny Jones
Lorry Sue

I need to create an if statement -
If Left(str,1) <> &quot;J&quot; then
skip this line and go to next line
end if

Real simple but can not find the right keyword.
Thanks
 
Line Input can read a text file one entire line at a time.
 
Right, but how do i force to go to the next line
 
not pretty but you could use the goto

ReadFile :

Line Input ...
If Left(str,1) <> &quot;J&quot; then
Goto ReadFile
end if
 
Do Until EOF(1)
Line Input #1, str
If Left(str,1) = &quot;J&quot; then 'or whatever
'Do processing
end if
Loop Tim

Remember the KISS principle:
Keep It Simple, Stupid! 8-)
 
If you use the File System Object, it has a method specifically for the job. fso.SkipLine. Check it out!
 
Here is a quick example program. I have a file with 7 lines in it. when it reads the file it skips 5.
1
2
3
4
5
6
7
Code:
Option Explicit
Private Sub Command1_Click()
'***BE SURE TO REFERENCE MICROSOFT SCRIPTING RUNTIME UNDER
'***UNDER PROJECT > REFERENCES
'''''''''''''''''''''''''''''''
Dim mFileSysObj As Scripting.FileSystemObject  ' General declaration
    Dim mFile As Scripting.File                        ' General declaration
    Dim mTextStream As Scripting.TextStream
 'Instantiating the object variable mFileSysObj
    Set mFileSysObj = New Scripting.FileSystemObject
    ' Get the file
    Set mFile = mFileSysObj.GetFile(App.Path & &quot;\&quot; & Trim(&quot;a.Txt&quot;))

    ' Open a text stream for reading from a file
    Set mTextStream = mFile.OpenAsTextStream(ForReading)
    
    'reading data from file as long as it is not the end of file
    While Not mTextStream.AtEndOfStream

        ' Read the data
     Combo1.AddItem (mTextStream.ReadLine)
        If (mTextStream.Line = 5) Then
            mTextStream.SkipLine
        
        End If
            
        'Parse the string as ',' as delimiter
       ' strArray = Split(strData, &quot;,&quot;)
         Wend
        
    Call mTextStream.Close     ' Close the text stream
    
End Sub

Private Sub Form_Load()
'setting the dialog path, the drive and the directory to the same path as the
'application
   ' dlgFile.InitDir = App.Path
    ChDrive App.Path
    ChDir App.Path
End Sub
 
Thanks it worked
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top