I am having trouble of making the vbscript to read the text line by line. This is the steps the code should do:
The line
Set listFile = fso.OpenTextFile(aniLines).ReadAll
won't work for two reasons:
Set
can only be used when assigning objects to variables, but ReadAll
returns a string.OpenTextFile()
expects a string, but aniLines
is an array of strings.To process all elements of the array, you could use something like this:
For Each line In aniLines
' read dirlist.txt file
Next
The question is how you want to read the dirlist.txt
files. You could do the same as you did with the other files: read the entire content of the file and split it into an array:
listFile = fso.OpenTextFile(line).ReadAll
listLines = Split(listFile, vbCrLf)
and then use another loop like the one above to process the fields of the array listLines
. This approach is preferrable for small files, because the code is simpler.
Or you could use the ReadLine
approach (which is preferrable when you have to process large files, because it avoids memory exhaustion):
Set listFile = fso.OpenTextFile(line) ' <-- remove .ReadAll from this line!
Do Until listFile.AtEndOfStream
fName = listFile.ReadLine
' do stuff with fName
Loop
I recommend using Do Until
instead of Do While Not
, because the semantic is the same, but the former is closer to natural language and thus easier to read.
Also it would be sufficient to instantiate a FileSystemObject
object once at the beginning of the script and just use that instance in the rest of the script. It's pointless to instantiate it over and over again.