Can I Read an Entire Text File Rather Than Reading It Line-by-Line?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! Right now I use a script to open a text file containing computer names. The script reads the first line of the file and connects to that computer, then reads the second line of the file and connects to that computer. Is there a way to store those names in a variable or something, so that I don’t have to keep reading the text file?

— KS

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KS. We’re assuming that your script currently looks something like this one. In this sample script, we open a text file (servers.txt), read the first line (we’re also assuming that there is one computer name per line in the file), and then echo the name of the computer. (In your script, of course, you probably connect to that remote computer at this point.) We then read in the second line of the text file, echo the name of that computer, loop around, read in the third line of the text file, and continue in this vein until we’ve finished reading each line in the file:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objTextFile = objFSO.OpenTextFile _ (“c:\scripts\servers.txt”, ForReading)

Do Until objTextFile.AtEndOfStream strComputer = objTextFile.ReadLine Wscript.Echo strComputer Loop

objTextFile.Close

Now, there’s nothing wrong with reading a text file this way; it works just fine. However, we can understand why you might want to read the text file all at once and then work with the list in memory. After all, suppose the text file is located on a remote computer. Not only will you have to continually travel across the network just to read a single line out of a text file, but if that remote computer goes down, then your script goes down as well.

So can you read the text file all at once, store that information in a variable, and then work with the list of computers from memory? Of course you can:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objTextFile = objFSO.OpenTextFile _ (“c:\scripts\servers.txt”, ForReading)

strText = objTextFile.ReadAll objTextFile.Close

arrComputers = Split(strText, vbCrLf)

For Each strComputer in arrComputers Wscript.Echo strComputer Next

Here’s how the script works. Like your current script, we create a constant (ForReading) and assign it the value 1; this is required when using the FileSystemObject to read a text file. We then create an instance of the FileSystemObject, and use the OpenTextFile method to open the file C:\Scripts\Servers.txt. But you already knew that, didn’t you?

Here’s the part you’re really interested in. Instead of reading the file line-by-line, we use the ReadAll method to read the entire text file in one fell swoop, storing the contents of that file in the variable strText. The variable strText now becomes an exact replica of the text file. Suppose your text file looks like this:

atl-ws-01
atl-ws-02
atl-ws-03
atl-ws-04

Now guess what will be displayed on screen if we echo the value of strText? That’s right:

atl-ws-01
atl-ws-02
atl-ws-03
atl-ws-04

Thus far we’ve succeeded in reading in the entire text file and storing it in a variable. However, that doesn’t really do us much good; after all, if we tried connecting to a computer using strText, we’d be connecting to the entire text file rather than a single computer, an operation doomed to fail. What we need to do now is somehow separate the individual computer names, so that we can then connect to the computers one at a time.

That’s what this line of code is for:

arrComputers = Split(strText, vbCrLf)

The Split command takes data (in this case, the variable strText) and creates an array out of it. How does it know where one computer name ends and the next begins? In our example, the “delimiter” that separates one computer from another is the carriage return linefeed; in our text file, we type one computer name, press ENTER, then type the next computer name. The VBScript constant vbCrLf used with the Split function represents the ENTER key. Suppose strText looked like this:

atl-ws-01,atl-ws-02,atl-ws-03,atl-ws-04

In that case, our delimiter would be the comma, and the Split command would look like this:

arrComputers = Split(strText, “,”)

In other words, pass Split the variable name and the delimiter, and it does the rest.

After we have our computer names safely stashed on an array, we can then iterate each item in that array using a For Each loop:

For Each strComputer in arrComputers
    Wscript.Echo strComputer
Next

Or for a somewhat more realistic example, here’s a sample script that uses WMI to connect to each computer in the text file and retrieve the name of the installed operating system:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objTextFile = objFSO.OpenTextFile _ (“c:\scripts\servers.txt”, ForReading)

strText = objTextFile.ReadAll objTextFile.Close

arrComputers = Split(strText, vbCrLf)

For Each strComputer in arrComputers

Set objWMIService = GetObject _ (“winmgmts:\\” & strComputer & “\root\cimv2”) Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_OperatingSystem”) For Each objItem in ColItems Wscript.Echo strComputer & “: ” & objItem.Caption Next

Next


0 comments

Discussion is closed.

Feedback usabilla icon