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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to go back a line

Status
Not open for further replies.

pivich

Technical User
Jan 24, 2007
11
CA
Hi!

How can you go back a line, when you are reading a textfile. I know there is a function to skip the line, but I haven't found a way to go back the line.

I need that because when script looks for certain expression in the text file and finds that expression, I need to record that line. But what happens is that when it finds the expression it goes on the next line.

Here's an example of the code

Code:
Do Until objFileforReading.AtEndOfStream
            strLine = objFileforReading.Readline
 
            strFind = "expression"
            If InStr(1,strLine,strFind, VBTEXTCOMPARE)>0 
               strLine = objFileforReading.Readline
               MsgBox strLine 
            End If
    Loop

Thank you!
 
This has been covered in several threads. Essentially read the entire file into an array. Then use a for loop to look at each line in the arry. When you find the line with the search string in it, the line one back is just the loop iterator minus one.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
Why not just do this?

Code:
Do Until objFileforReading.AtEndOfStream
	strLine = objFileforReading.Readline
	strFind = "expression"
	If InStr(1,strLine,strFind, VBTEXTCOMPARE)>0 Then
		MsgBox strLine
	End If
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
If I understand the question he wants to get the line before the line with the string in it.

[red]"... isn't sanity really just a one trick pony anyway?! I mean, all you get is one trick, rational thinking, but when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick[/red]
 
store the previous line in a temp variable each time...

--------------------
Procrastinate Now!
 
I may have misunderstood because of this line:
"certain expression in the text file and finds that expression, I need to record that line"

The method he posted reads two lines per loop and therefore finds the need to go back, when he may not have to. Of course if he does need/want to go back, the suggestions posted will do the trick.

Do Until objFileforReading.AtEndOfStream
strLine = objFileforReading.Readline 'read line one

strFind = "expression"
If InStr(1,strLine,strFind, VBTEXTCOMPARE)>0
strLine = objFileforReading.Readline 'read line two
MsgBox strLine
End If
Loop

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top