Reading a text file with FileSystemObject (1 Viewer)

jal

Registered User.
Local time
Today, 05:07
Joined
Mar 30, 2007
Messages
1,709
I'm doing a VBscript. I have had some success so far, with populating recordsets and moving files.

The next thing I need to do is read a textfiile. The code I am using seems to be a textbook example but I can't get it to work.


Set fso = CreateObject("Scripting.FileSystemObject")
Set stream = fso_OpenTextFile(pathToTextFile, 1) '1 means ForReading
Dim Ln
Do Until stream.AtEndOfStream
Ln = stream.ReadLine
Loop



I purchased a VBscript editor which has a debugger. When I step through the code, I verified that "PathToTextFile" has the correct value and I verified that this textfile does indeed contain a lot of text.

The code isn't throwing any errors. However, the variable "Ln" remains an empty string. It's not reading the text. It's just a regular plain text file editable in Wordpad or Notepad.
 

jal

Registered User.
Local time
Today, 05:07
Joined
Mar 30, 2007
Messages
1,709
Here's a sample I found in a tutorial - it's the same code as mine but mine isn't working:

Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objTextFile = objFSO.OpenTextFile ("c:\scripts\servers and services.txt", ForReading)
Do Until objTextFile.AtEndOfStream
strNextLine = objTextFile.Readline
'more code
Loop

From here:
http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/other/textfiles/

I also stepped through my code to make sure that the loop is iterating. The ReadLine is executing time and again in the loop but for some reason the Ln variable in my code isn't populating.
 

jal

Registered User.
Local time
Today, 05:07
Joined
Mar 30, 2007
Messages
1,709
Seems to be working now, can't say I understand it.

Apparently I had to set the "Format" value (the third param in OpenTextFile). I set it to negative 2 (means TristateUseDefault) and it worked.
 

Users who are viewing this thread

Top Bottom