Link to home
Start Free TrialLog in
Avatar of UniqueData
UniqueDataFlag for United States of America

asked on

Open text file and read twice

I have code that reads a text file line by line and does record appends.  I would like to show the users a 'status bar' indicating how far along they are.  What I am planning on doing is looping through the text file once until EOF(1) to find the total number of lines.  But how to I get back to the top of the text file to start running through with my appends?  Is there a .MoveToTop type command for an open file (other than close the file then reopen)?

Thanks in advance,

Michael
Avatar of DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
DatabaseMX (Joe Anderson - Former Microsoft Access MVP)
Flag of United States of America image

If you have a recordset open, you can user MoveFirst.  Or, you can create a line Label ... and then a Goto LabelName.

Can you post the code you have - and exactly where / when you need to go to the 'top' ?

mx
Avatar of UniqueData

ASKER

Basically I want:
Dim intLineCount as integer

intLineCount = 0
Open strFileName for Input as #1
While not EOF(1)
   Line Input #1, strLine
   intLineCount = intLineCount+1
Loop

'Then code to go back to top of #1 and loop again with my line inspections and recordset appends/updates.  

intCurrentLineCount =0
While Not EOF(1)                      'this would not work if I did not first get back to the 'top' of the text file
   Line Input #1, strLine
   If left$(strLine,1) = ......
      blah blah blah
      intCurrentLineCount =intCurrentLineCount +1
     'now I know what percentage is done ( intCurrentLineCount / intLineCount)
     ' and I can display this to the user
Loop  
"to go back to top of #1"

Sorry ... not sure where you mean ?

mx
Sorry, not quite sure the terminology here...

I am opening a text file for Input as #1
I want to loop through the entire text file once to count the total number of lines in the text file (unless there is another way to get the line count of a text file)
Then I want to loop through again (from line 1) to do my string manipulations.  But I can't loop through again without 'moving' to the beginning of the text file because in my second time looping I start off already at EOF(1) from the first looping.

The quick and dirty way would be to:
Open strFileName for Input as #1
Loop first time to get the line count
Close #1
then...
Open strFileName for Input as #1  ' (again)
Loop second time to do the time consuming work
Close #1

but I figured there was a better way than to close and reopen.
ASKER CERTIFIED SOLUTION
Avatar of UniqueData
UniqueData
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial