[ACCEPTED]-Read lines from a text file but skip the first two lines-text-files
Accepted answer
That whole Open <file path> For Input As <some number>
thing is so 1990s. It's also slow 7 and very error-prone.
In your VBA editor, Select 6 References from the Tools menu and look 5 for "Microsoft Scripting Runtime" (scrrun.dll) which 4 should be available on pretty much any XP 3 or Vista machine. It it's there, select 2 it. Now you have access to a (to me at least) rather 1 more robust solution:
With New Scripting.FileSystemObject
With .OpenTextFile(sFilename, ForReading)
If Not .AtEndOfStream Then .SkipLine
If Not .AtEndOfStream Then .SkipLine
Do Until .AtEndOfStream
DoSomethingImportantTo .ReadLine
Loop
End With
End With
You can use random access.
Open "C:\docs\TESTFILE.txt" For Random As #1
Position = 3 ' Define record number.
Get #1, Position, ARecord ' Read record.
Close #1
0
Open sFileName For Input As iFileNum
Dim LineNum As Long
LineNum = 0
Do While Not EOF(iFileNum)
LineNum = LineNum + 1
Line Input #iFileNum, Fields
If LineNum > 2 Then
DoStuffWith(Fields)
End If
Loop
0
May be I am oversimplifying?
Just add the 1 following code:
Open sFileName For Input as iFileNum
Line Input #iFileNum, dummy1
Line Input #iFileNum, dummy2
........
Sundar
Dim sFileName As String
Dim iFileNum As Integer
Dim sBuf As String
Dim Fields as String
Dim TempStr as String
sFileName = "c:\fields.ini"
''//Does the file exist?
If Len(Dir$(sFileName)) = 0 Then
MsgBox ("Cannot find fields.ini")
End If
iFileNum = FreeFile()
Open sFileName For Input As iFileNum
''//This part skips the first two lines
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
if not(EOF(iFileNum)) Then Line Input #iFilenum, TempStr
Do While Not EOF(iFileNum)
Line Input #iFileNum, Fields
MsgBox (Fields)
Loop
0
Source:
stackoverflow.com
More Related questions
Cookie Warning
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.