Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Read from a text file. 3

Status
Not open for further replies.

Curtis360it

IS-IT--Management
Nov 7, 2005
88
0
0
US
How in vbscript do you go about reading from a textfile?

The file is an example textfile, it will be dynamic in the amount of lines:

cwilliams@williamsit.com
jdoe@aol.com
sdodd@yahoo.com
etc.
etc.
etc.
 
Awesome, was looking for docs on the subject! Thanks!
 
Something like this maybe.

Code:
Dim arrFileLines()
i = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)

Do Until objFile.AtEndOfStream
     Redim Preserve arrFileLines(i)
     arrFileLines(i) = objFile.ReadLine
     i = i + 1
Loop

objFile.Close

For l = Lbound(arrFileLines) to UBound(arrFileLines) Step 1
    Wscript.Echo arrFileLines(l)
Next


Thanks

John Fuhrman
Titan Global Services
 
John, if you really prefer to deal with arrays, then I suggest you to use the ReadAll method and the Split function.
 
PHV, I didn't explore splitting the text up because he only said he wanted a way to read a text file. And the script was one I had handy.

One question for you PHV, if his file gets quite large isn't the ReadAll method more memory hungry then the Do Until AtEndofStream? I haven't ever tried to use the ReadAll with a large file before.

As PHV points out there are many way to do what you ask and the best method will depend on how you want to use what is in the text file being read and on how big the file is or will get.

Thanks PHV, I could have commented better! [thumbsup2]

Thanks

John Fuhrman
Titan Global Services
 
Ooops, should have looked before typing. [blush]

Code:
Dim arrFileLines()
i = 0

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\FSO\ScriptLog.txt", 1)

Do Until objFile.AtEndOfStream
	WScript.Echo objFile.ReadLine
Loop

objFile.Close

Skipped the array. [wink]

Thanks

John Fuhrman
Titan Global Services
 
OK, One using ReadAll.
Very simple, No text manipulation. Just display the contents of the array from the ReadAll.

Thanks again PHV, I will have to play with this some more and see if this will work better in one of my FTP scripts rather than the exist read stream to OEF. (Using it to pass multiple command line options to another script multiple times.)

FTP file location,UserName,Password,Institution

There are currently 25 lines being parsed.

Oh and the script uses the API for CuteFTP. Simple but neat and works well.

Code:
dim filesys, text, readfile, contents
set filesys = CreateObject("Scripting.FileSystemObject")
set readfile = filesys.OpenTextFile("C:\FSO\ScriptLog.txt", 1, false)
contents = readfile.ReadAll
readfile.close
WScript.Echo "The file contains the following text - " & vbCrLf &_
contents


Thanks

John Fuhrman
Titan Global Services
 
sparkbyte,

You are correct about using the loop for large files...some interesting read:

For those that use the ReadAll in conjunction with the Split function to create an array...just remember empty lines will result in elements with no values...so sometimes it is just safer to loop through the lines and act accordingly. It really depends on what you're trying to do.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top