Link to home
Start Free TrialLog in
Avatar of jrslim
jrslim

asked on

How can I reset the "AtEndOfStream" status of an OpenTextFile in VBScript?

As in the following example, when I pass the first array item to a do loop referencing an OpenTextFile, after the do loops iteration using the first array item the objArrFile.AtEndOfStream = True and remains so in subsequent calls of the subroutine.  I can reopen the file (Set objFile = objFSO.OpenTextFile("C:\read.txt", ForReading) at the beginning of the sub which makes it work, but I think there is probably a cleaner method.  I need to use this same file in the main script as well as subroutines and functions.

Const ForReading = 1
Dim objFile

'Open Read file for DO loop
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\read.txt", ForReading)

'Open read file for array
Set objArrFSO = CreateObject("Scripting.FileSystemObject")
Set objArrFile = objArrFSO.OpenTextFile("c:\Array.txt", ForReading)

'Create Array by reading array file
Dim arrFileLines()
i = 0
Do Until objArrFile.AtEndOfStream
   Redim Preserve arrFileLines(i)
   arrFileLines(i) = objArrFile.ReadLine
   i = i + 1
Loop
 
For Each arrItem in arrFileLines
   concat(arrItem)
Next

Sub concat(arrItem)

  'This statement resets to beginning of file
  'Set objFile = objFSO.OpenTextFile("C:\read.txt", ForReading)

  Do Until objFile.AtEndOfStream
     strListItem = objFile.ReadLine
     wscript.echo "Array Item " & arrItem & " with List Item " &  StrListItem
  Loop
End Sub
Avatar of Bill Prew
Bill Prew

I suspect the proper way is to Close it and re-open it.  I don't think there is a "rewind" command in vbscript.

~bp
With the sample you showed, is this a real example of what you need to do.  This looks like you want to display the "pairings" of every line in the first file, with the second?  If that is the case, there is a better way to do that I believe.

~bp
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

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
Avatar of jrslim

ASKER

Thanks bp, the double array looks pretty clean.
Great, glad that was helpful, thanks.

~bp