# 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.

Dim objFile

'Open Read file for DO loop
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set objArrFSO = CreateObject("Scripting.FileSystemObject")

'Create Array by reading array file
Dim arrFileLines()
i = 0
Do Until objArrFile.AtEndOfStream
Redim Preserve arrFileLines(i)
i = i + 1
Loop

For Each arrItem in arrFileLines
concat(arrItem)
Next

Sub concat(arrItem)

'This statement resets to beginning of file

Do Until objFile.AtEndOfStream
wscript.echo "Array Item " & arrItem & " with List Item " &  StrListItem
Loop
End Sub
###### Who is Participating?

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 ArrayFile = "x:\ee\ee26919345\array.txt"

' Define all needed variables
Dim objFSO, objFile

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

'Open first file and load into array
If Right(strData, 2) = vbCrLf Then strData = Left(strData, Len(strData) - 2)
objFile.Close

'Open second file and load into array
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
Wscript.Echo "Array Item " & strArrayItem & " with List Item " &  strReadItem
Next
Next


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 ArrayFile = "x:\ee\ee26919345\array.txt"

' Define all needed variables

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

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

~bp
0

Commented:
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

Commented:
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

Author Commented:
Thanks bp, the double array looks pretty clean.
0

Commented:

~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.