October 21st, 2005

How Can I Read a Text File on a Remote Computer?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I read a text file on a remote computer?

— BM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BM. We have a confession to make: we’re using you and your question for our own nefarious purposes. Earlier this week we answered a question about reading the last line in a group of text files; in our answer we promised we’d show people how to use the FileSystemObject to perform that same feat on a remote computer. That was fine, except that – in order to maintain the spirit of this column – we couldn’t do that unless we had a question about working with text files on a remote computer. So we chose yours.

So, yes, we’re using you. But on the bright side, you’ll get your question answered. In fact, let’s start with your question first.

As you know, the FileSystemObject (the object used for reading and writing text files) is designed to work locally; in fact, pretty much anything and everything you read about the FileSystemObject will take great pains to point out that you can’t use the object against remote machines. As it turns out, though, that’s not entirely true: that’s because the FileSystemObject is capable of using UNC paths. Suppose the file you want to read is located on a file share (\\atl-fs-01\public\myfile.txt). In that case, opening and reading the text file is as easy as this:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objTextFile = objFSO.OpenTextFile _ (“\\atl-fs-01\public\myfile.txt”, ForReading) strContents = objTextFile.ReadAll objTextFile.Close

Wscript.Echo strContents

As you can see, we begin by defining a constant named ForReading and setting the value to 1. We then create the FileSystemObject reference and call the OpenTextFile method, passing two parameters: the UNC path to the file in question, and the constant ForReading. At this point we can do anything we want with the file: Just to give you a quick example of working with a text file we call the ReadAll method to read the entire contents of the file into a variable named strContents. We then close the file, echo the value of strContents, and we’re done.

Now, this works great as long as the file in question is in a shared folder. But what if the file is not in a shared folder? In that case, your only recourse is to use the Administrative shares (like C$). This script will read the file MyFile.txt even if the folder C:\Public has not been shared:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objTextFile = objFSO.OpenTextFile _ (“\\atl-fs-01\C$\public\myfile.txt”, ForReading) strContents = objTextFile.ReadAll objTextFile.Close

Wscript.Echo strContents

If you don’t use Administrative shares, well, then you’re probably out of luck. (Unless you do something really crazy, like use the WSHController object. But that’s a story for another time.)

And that brings us to our ulterior motive: how can we cycle through all the files in a remote folder and use the FileSystemObject to open and read each of those files? Well, here’s one way:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

strComputer = “atl-fs-01”

Set objWMIService = GetObject(“winmgmts:\\” & strComputer & “\root\cimv2”)

Set colFileList = objWMIService.ExecQuery _ (“ASSOCIATORS OF {Win32_Directory.Name=’C:\Logs’} Where ” _ & “ResultClass = CIM_DataFile”)

For Each objFile In colFileList strFilePath = “\\” & strComputer & “\C$\Logs\” & _ objFile.FileName & “.” & objFile.Extension Set objTextFile = objFSO.OpenTextFile(strFilePath, ForReading) strContents = objTextFile.ReadAll Wscript.Echo strContents objTextFile.Close Next

What we’re doing here is connecting to the remote computer atl-fs-01 and retrieving a collection of all the files in the folder C:\Logs. The tricky part lies in constructing a path to each file; that’s because we need to use an Administrative share path similar to this:

\\atl-fs-01\C$\Logs\MyFile.log

To construct that path we use a little bit of WMI and a little bit of hard-coding:

strFilePath = “\\” & strComputer & “\C$\Logs\” & _
    objFile.FileName & “.” & objFile.Extension

What we’re doing is this:

Starting with a pair of \’s: \\

Adding the name of the computer: \\atl-fs-01

Adding a \ and the Administrative share path C$\Logs\: \\atl-fs-01\C$\Logs\

Adding the WMI FileName property (which is just the name, minus the file extension): \\atl-fs-01\C$\Logs\MyFile

Adding the period between the file name and the file extension (because the period is not part of WMI’s Extension property): \\atl-fs-01\C$\Logs\MyFile.

Adding the WMI property Extension: \\atl-fs-01\C$\Logs\MyFile.log

It’s a tad bit complicated, but it builds the UNC path we need. Furthermore, each time through the loop it will substitute a new file name and new file extension (the computer name and the folder path never change). Thus we’ll eventually be able to open – and read – each file in the remote folder.

By the way, thanks for letting us take advantage of your question, BM. We owe you one!

Author

0 comments

Discussion are closed.