How Can I Map Drive X on a Computer, But Only If Drive X isn’t Already in Use?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I map drive X on a computer, but only if drive X isn’t already in use?

— PN

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PN. You know, this is almost too good to be true (but, sadly, it is true). The Scripting Guy who writes this column was busy writing this column. And because today just happens to be Friday the 13th the theme for the day was, as you might expect, Friday the 13th.

In typical Hey, Scripting Guy! fashion, he began by recounting a little bit of the history of both Friday and of the number 13. For example, in both Hindu and Norse culture, it’s unlucky to invite 13 people over for dinner (especially if you’re the one who’s supposed to do all the cooking). In the Middle Ages 13 was considered bad because witches supposedly met in groups of 13: 12 witches plus the Devil.

Note. The Scripting Editor tells us that this is ridiculous; after all, her coven hardly ever has 13 people at a meeting.

Likewise, Fridays have long been viewed with a jaundiced eye. For example, tradition holds that Adam and Eve first tasted the forbidden fruit on a Friday; needless to say, that didn’t work out too well for the two of them. In ancient Rome Fridays served as Execution Day, something that was apparently so popular that the British adopted the idea and turned every Friday into Hangman’s Day.

Here at Microsoft we’ve carried on that tradition in equally-ominous fashion: Friday is a popular day for team meetings.

At any rate, the Scripting Guy who writes this column went on to relate that it was inevitable that Friday – the worst day of the week – would eventually be paired with 13, the unluckiest number. The net result? Friday the 13th, a day when something terrible is bound to happen. At that point, of course, the Scripting Guy who writes this column was about to dismiss this all as superstitious nonsense when it happened: his screen went black, the computer rebooted itself, and, upon restarting, displayed this message:

Non-system or disk error.


Believe it or not, in the middle of writing a column debunking the idea of Friday the 13th being unlucky our computer died. If it wasn’t our computer we’d find that kind of funny. (The Scripting Editor found it somewhat amusing.)

Note. No, we don’t believe this was karma, either. However, we aren’t going to say that just in case it was karma. We’re having enough problems today without adding bad karma to the mix.

So do we still think that this notion about Friday the 13th being unlucky is nothing but superstitious nonsense? Of course. After all, Friday the 13th might not have been particularly lucky for us (or at least for our computer), but it turned out to be plenty lucky for PN:

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_LogicalDisk Where DeviceID = ‘X:'”)

If colItems.Count = 0 Then Set objNetwork = CreateObject(“Wscript.Network”) objNetwork.MapNetworkDrive “X:”, “\\atl-fs-01\public” End If

As you can see, this is a pretty easy little script to write. (Which is good, seeing as we no longer have a computer on which to write it.) We start out, as we so often do, by connecting to the WMI service on the local computer. Could we run this same script on a remote computer? Well, maybe. It’s easy enough to determine whether drive X is in use on a remote machine; unfortunately, it’s a bit harder to map a drive on a remote machine. That’s because Windows Script Host, and the WSH Network object, are designed to work locally rather than remotely. (And yes, it is interesting that something named the Network object can’t function over the network, isn’t it?) On the other hand, you can always find workarounds for these things; for one suggestion, see this previous Hey, Scripting Guy! column.

After we make the connection we then use the ExecQuery method, and the following line of code, to return a collection of all the drives on the computer that have a DeviceID equal to X:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_LogicalDisk Where DeviceID = ‘X:'”)

Why do we do that? Well, this is a quick and easy way of determining whether drive X is already in use. If drive X is already mapped (to either a physical drive or a network drive) then we’ll end up with one item in our collection. If drive X is not in use (meaning that the drive letter is available to us) then we’ll have zero items in our collection.

With that in mind, our next step is to check the collection Count to determine whether any drive Xs are in use:

If colItems.Count = 0 Then

If the Count is equal to 0, then drive X is available. Therefore, we go ahead and create an instance of the Wscript.Network object and then use the MapNetworkDrive method to map the folder \\atl-fs-01\public to drive X:

Set objNetwork = CreateObject(“Wscript.Network”)
objNetwork.MapNetworkDrive “X:”, “\\atl-fs-01\public”

What if the Count isn’t equal to 0? Well, that can only mean one thing: drive X must already be in use. Therefore, and per PN’s wishes, we don’t do anything at all.

Note. If drive X is in use wouldn’t it be better to go ahead and map the drive to the next available drive letter? Maybe. If that’s something you’d like to do then you’re in luck, too: we just happen to have another column on drive mapping, this one showing how you can determine the next available drive letter on a computer.

Anyway, we hope that helps, PN. And like we said, don’t worry too much about it being Friday the 13th; theScripting Guys don’t believe that Friday the 13th is any worse than any other Friday. After all, being a Friday (13th or otherwise) meant that we had to come in to work today. That pretty much ruled out this being a lucky day right there, regardless of whether our not any of our computers died.


Discussion is closed.

Feedback usabilla icon