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
jrslimAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
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
0
Bill PrewCommented:
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
0
Bill PrewCommented:
Here's an easier way to get the text file contents into an array, and by placing both file contents into arrays you can just loop over them as many times as you need to.  This would be faster than closing and opening the file multiple times to reprocess the same data.

Option Explicit

' Define needed constants
Const ForReading = 1
Const ReadFile = "x:\ee\ee26919345\read.txt"
Const ArrayFile = "x:\ee\ee26919345\array.txt"

' Define all needed variables
Dim objFSO, objFile
Dim arrRead, arrArray
Dim strData, strArrayItem, strReadItem

' Create file system object
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open first file and load into array
Set objFile = objFSO.OpenTextFile(ReadFile, ForReading)
strData = objFile.ReadAll
If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
arrRead = Split(strData, vbCrLf)
objFile.Close

'Open second file and load into array
Set objFile = objFSO.OpenTextFile(ArrayFile, ForReading)
strData = objFile.ReadAll
If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
arrArray = Split(strData, vbCrLf)
objFile.Close

' Itterate through all members of both arrays
For Each strArrayItem in arrArray
   For Each strReadItem in arrRead
      Wscript.Echo "Array Item " & strArrayItem & " with List Item " &  strReadItem
   Next
Next

Open in new window


And if you preferred, you could even handle the reading of the files and loading into arrays in a function, so that the main routine gets cleaner:

Option Explicit

' Define needed constants
Const ForReading = 1
Const ReadFile = "x:\ee\ee26919345\read.txt"
Const ArrayFile = "x:\ee\ee26919345\array.txt"

' Define all needed variables
Dim arrRead, arrArray
Dim strArrayItem, strReadItem

' Load files into arrays
arrRead = LoadFile(ReadFile)
arrArray = LoadFile(ArrayFile)

' Itterate through all members of both arrays
For Each strArrayItem in arrArray
   For Each strReadItem in arrRead
      Wscript.Echo "Array Item " & strArrayItem & " with List Item " &  strReadItem
   Next
Next

Function LoadFile(strFile)
   Dim objFSO, objFile, strData
   ' Create file system object
   Set objFSO = CreateObject("Scripting.FileSystemObject")
   ' Open file and load into array
   Set objFile = objFSO.OpenTextFile(strFile, ForReading)
   strData = objFile.ReadAll
   If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
   LoadFile = Split(strData, vbCrLf)
   objFile.Close
   ' Cleanup
   Set objFile = Nothing
   Set objFSO = Nothing
End Function

Open in new window

~bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jrslimAuthor Commented:
Thanks bp, the double array looks pretty clean.
0
Bill PrewCommented:
Great, glad that was helpful, thanks.

~bp
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.