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?
 
Bill PrewConnect With a Mentor Commented:
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
 
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
 
jrslimAuthor Commented:
Thanks bp, the double array looks pretty clean.
0
 
Bill PrewCommented:
Great, glad that was helpful, thanks.

~bp
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.