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:\re ad.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.Fi leSystemOb ject")
Set objFile = objFSO.OpenTextFile("C:\re ad.txt", ForReading)
'Open read file for array
Set objArrFSO = CreateObject("Scripting.Fi leSystemOb ject")
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:\re ad.txt", ForReading)
Do Until objFile.AtEndOfStream
strListItem = objFile.ReadLine
wscript.echo "Array Item " & arrItem & " with List Item " & StrListItem
Loop
End Sub
Const ForReading = 1
Dim objFile
'Open Read file for DO loop
Set objFSO = CreateObject("Scripting.Fi
Set objFile = objFSO.OpenTextFile("C:\re
'Open read file for array
Set objArrFSO = CreateObject("Scripting.Fi
Set objArrFile = objArrFSO.OpenTextFile("c:
'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:\re
Do Until objFile.AtEndOfStream
strListItem = objFile.ReadLine
wscript.echo "Array Item " & arrItem & " with List Item " & StrListItem
Loop
End Sub
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
~bp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks bp, the double array looks pretty clean.
Great, glad that was helpful, thanks.
~bp
~bp
~bp