Kiolul
Kiolul

Reputation: 1

AtEndOfStream condition error in vbscript

I encounter a problem with a double loop in a vbs script.

This is the script:

Set fso = CreateObject("Scripting.FileSystemObject")
Set sourceFile = fso.OpenTextFile("source.txt")
Set validFile = fso.OpenTextFile("valid.txt")
Set result = fso.CreateTextFile("result.txt", True)

Do while validFile.AtEndOfStream <> True
Wscript.echo validFile.AtEndOfStream
  validLine = validFile.ReadLine
  Do while sourceFile.AtEndOfStream <> True
    sourceLine = sourceFile.ReadLine
    If InStr(sourceLine,validLine) > 0 then
        result.WriteLine(sourceLine)
    End if 
  Loop
sourceFile.Close
Loop
Wscript.echo "End of script"
result.close
validFile.close

The second Loop will not be reset at its end, so the next first loop start, it does nothing. The sourceFile.AtEndOfStream return -1 unlike 0. I have put sourceFile.Close wich seems to do a reset but nothing append.

Thx for your help.

Upvotes: 0

Views: 7457

Answers (4)

Kiolul
Kiolul

Reputation: 1

Thx for your help. Indeed, I had not enough time to do things well but I have completely rewrite this little script with your suggestions and it works great!!

Set fso = CreateObject("Scripting.FileSystemObject")
Set validFile = fso.OpenTextFile("valid.txt")
Set result = fso.CreateTextFile("result.txt", True)
sourceText = Split(fso.OpenTextFile("source.txt").ReadAll, vbNewLine)

Do until validFile.AtEndOfStream

  validLine = validFile.ReadLine

  For Each sourceLine In sourceText

    If InStr(sourceLine,validLine) > 0 then
        result.WriteLine(sourceLine)
    End if 

  Next

Loop

Wscript.echo "Fin du traitement"
result.close
validFile.close

Bye.

Upvotes: 0

Ekkehard.Horner
Ekkehard.Horner

Reputation: 38755

While you can read each line from one (source) file and loop over all lines of a second file (valid) to use each line in valid on each line in source by opening/closing valid in the outer loop, you shouldn't.

Load the content of valid into memory before you start the loop on source.

If you give some details about your real world problem - what do you want to check source for? - we could discuss how and into what to load valid.

Upvotes: 1

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200393

When you read a file in a loop, the cursor (i.e. the position in the file from which you currently read) doesn't magically return to the beginning of the file after the loop ends. Particularly not when you close the file after the loop (because then you don't even have a file anymore). You need re-open it before you can read it again. Also, I'd suggest using Until condition instead of While condition <> true as the former is much better readable than the latter:

Do Until validFile.AtEndOfStream
  validLine = validFile.ReadLine

  Set sourceFile = fso.OpenTextFile("source.txt")
  Do Until sourceFile.AtEndOfStream
    ...
  Loop
  sourceFile.Close
Loop

However, as Ekkehard.Horner pointed out, reading a file over and over again is usually not a good choice performance-wise. If at least one file is relatively small (compared to the available amout of RAM) it's better to read that file into an array and iterate over that array in the inner loop:

sourceText = Split(fso.OpenTextFile("source.txt").ReadAll, vbNewLine)

Do Until validFile.AtEndOfStream
  validLine = validFile.ReadLine
  For Each sourceLine In sourceText
    ...
  Next
Loop

Upvotes: 1

langstrom
langstrom

Reputation: 1702

What is it that you're trying to do?

I can see at least one problem here right off. If you do sourceFile.Close then try to read it on the next loop, you won't be able to because it's closed.

Try moving this line
Set sourceFile = fso.OpenTextFile("source.txt")
right before this line in the script
Do while sourceFile.AtEndOfStream <> True

You'll be opening and closing the file at each loop, which is for each line in the valid.txt file. This doesn't seem like good practice, but if you explain what your goal is maybe I could help find a better way.

Upvotes: 0

Related Questions