May 8th, 2006

How Can I Map Network Drives Based on a Computer’s Site?

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I parse information in a text file, and then use that information to map network drives based on a computer’s site?

— BH

SpacerHey, Scripting Guy! AnswerScript Center

Hey, BH. We’ll be with you in a second – we’re still replaying last night’s baseball game, in which the Kirkland Fire dropped a tough 7-5 decision to a very good team from Bothell. Bottom of the sixth, runners on second and third, nobody out, and we failed to score: a missed bunt and then a strikeout, a popup to the infield, and a weak grounder to second base. And all the Scripting Coach could do was sit there and watch.

Which, come to think of it, is pretty much what he does at work all day, too.

Still, what’s done is done, and it’s time to move on to more important things, things like Sunday’s game with Issaquah. But because we have some time to kill between now and then, we decided to go ahead and see if we could answer your question as well.

To begin with, let’s provide everyone with a little background information. According to your email the text file that contains the drive mapping information looks something like this:

Site1 X: \\atl-fs-01\public
Site1 Y: \\atl-fs-01\users
Site2 X: \\atl-fs-01\users
Site2 Y: \\atl-fs-02\managers
Site3 X: \\atl-fs-02\users
Site3 Y: \\atl-fs-03\reports

The basic idea here is that after you’ve determined the site that the computer is in (something we won’t deal with today, seeing as how we don’t have a copy of the code you’re using to determine the site) you then want to read the text file and map the drives appropriate for that site. For example, if a computer is in Site2 you’ll want to map these two drives:

Drive X, mapped to \\atl-fs-01\users

Drive Y, mapped to \\atl-fs-02\managers

How do you do that in a script? Here’s one way:

Const ForReading = 1

Set objFSO = CreateObject(“Scripting.FileSystemObject”) Set objNetwork = CreateObject(“Wscript.Network”)

Set objFile = objFSO.OpenTextFile(“c:\scripts\test.txt”, ForReading)

Do Until objFile.AtEndOfStream strLine = objFile.ReadLine arrItems = Split(strLine, ” “) If arrItems(0) = “Site1” Then objNetwork.MapNetworkDrive arrItems(1), arrItems(2) End If Loop

objFile.Close

Good question: how does this script work? Well, it starts off by defining a constant named ForReading and setting the value to 1; we’ll use this constant when we read from the text file. We then create two objects: the Scripting.FileSystemObject (which we use to work with the text file) and the Wscript.Network object, which we use to map the drives.

Note. This is probably a good time to point out that this script needs to run on the local computer; that’s because neither the FileSystemObject nor the Network object are designed to work against remote machines. That shouldn’t be a problem for you, BH, because it sounds like this script is running as a logon script, in which case it always runs on the local machine.

Can you feel the excitement in the air as we make our way through the code? Well, then hold onto your hat, because now we are – drumroll, please – ready to open our text file (C:\Scripts\Test.txt) for reading:

Set objFile = objFSO.OpenTextFile(“c:\scripts\test.txt”, ForReading)

Once the text file is open we are, in turn, open for business. To begin with, we set up a Do Until loop that we’ll use to read the text file line-by-line, continuing until there are no more lines left to read (or, as we script writers like to put it, until the AtEndOfStream property is true). Inside that loop we call the ReadLine method to read the first line from the text file and store that value in the variable strLine. That means that, the first time through the loop, strLine will be equal to this:

Site1 X: \\atl-fs-01\public

Another good question: now what do we do? Well, as you can see, all the information we need – site name, drive letter, network path – is included in strLine; the only problem is that these three values have all been glommed together in a single string. But that’s OK; all we have to do is un-glom them using the VBScript Split function:

arrItems = Split(strLine, ” “)

All we’re doing here is asking VBScript to take the value in strLine and split that value into separate elements (with each element being stored in an array named arrItems). How does the Split function know where one element ends and the next one begins? Because we tell it: the second parameter we pass to Split is simply a blank space (“ “). That tells the function to “split” the string each time it encounters a blank space. The net result? The array arrItems ends up with the following three elements, which correspond quite nicely to the three pieces of information we need to map drives:

Site1

X:

\\atl-fs-01\public

The rest is easy. In a VBScript array the first item in an array is always given an index number of 0. By checking the value of item 0 we can determine whether the information we just read in from the text file happens to be for Site1:

If arrItems(0) = “Site1” Then

In this case, it is, which means we go ahead and use the remaining two elements in the array to map the drive. The MapNetworkDrive method requires two parameters: the drive letter and the network path. Because we know the drive letter (it’s the second item in the array, which has an index number of 1) and the network path (it’s item 3 in the array, with an index number of 2) we can map the drive with just a single line of code:

objNetwork.MapNetworkDrive arrItems(1), arrItems(2)

And there you have it. We loop around, read the next line in the text file, and repeat the process. Because only two lines in our text file are appropriate for Site1, when the script finishes we should have mapped two drives, one to \\atl-fs-01\public (drive X) and one to \\atl-fs-01\users (drive Y). That’s all there is to it.

As for the more important matter – baseball – well, easy come, easy go. Did we mention that the Scripting Son went 4-for-4 with 3 runs batted in? And yet, even though he reached base four times, he never once made it as far as third, let alone score a run. It was that kind of night. But, hey, we’ll get ‘em on Sunday. Guaranteed.

Note. Guarantee not guaranteed. After all, when you’re dealing with 15- and 16-year-olds you should never guarantee anything.

Author

0 comments

Discussion are closed.