How Can I Pass a Drive Letter From a Script to the Command Shell?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I pass the drive letter of a drive from a VBScript script to the command shell that called that script?

— JF

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JF. You know, as kids grow up they pass through a phase where they believe that their parents are capable of doing – and of fixing – anything. Admittedly, children quickly learn the cold, harsh facts, but, at least for a little while, kids believe that parents can do no wrong.

Of course, as a parent that’s incredibly heart-warming; at the same time, however, it places a heck of a lot of pressure on you. After all, you feel obligated to live up to the faith that has been placed in you. As a result, you end up spending a ridiculous amount of time fiddling with duct tape and Super Glue, all in an often-futile attempt to fix something that you know can’t be fixed. But with little Billy or little Betty counting on you, well, what else can you do?

Note. To tell you the truth, we have no idea why little Billy and little Betty are always bringing their broken toys over here. Why don’t they ask their own parents for help?

Here at Hey, Scripting Guy! we think of our readers as being like our own children. Granted, children who never call, children who never remember our birthdays, and children who only come around when they need something from us. But, hey, children nonetheless. And so, as Scripting Guys, we feel obligated to get out the duct tape and Super Glue and see if we can figure out a way to pass the drive letter of a drive from a VBScript script to the command shell.

Before anyone asks, we should mention that JF noted that he can’t write the drive letter to a file or the registry. That took care of our Plan A. And while he didn’t specifically say this, we’re also assuming that he’d like the script to actually work; that eliminated our Plan B. Fortunately, Plan C seems to do the trick:

strDrive = “D:”
strDrive = Left(strDrive,1)
intDrive = Asc(strDrive) – 64


What we have here is a very simple – but semi-clever – way to work around the limitations placed on us. We were aware that you can use Windows Script Host’s Quit method to set the value of the errorlevel environment variable. As far as we knew, however, you could write only numeric values to errorlevel. As it turns out – and for once – we were right about that.

Which meant that we had to find a way to work around that limitation as well. But hey, Scripting Guys – like parents – can do anything, right?

The script itself starts out by assigning an arbitrary drive letter to a variable named strDrive. (We’re assuming that, in a real script, you’d have real code that goes out and determines the drive letter, perhaps WMI code that identifies the drive letter for your CD-ROM drive.) After assigning the value D: to strDrive we then use this line of code to grab just the first letter in that value:

strDrive = Left(strDrive,1)

Why? That’s an easy one: we only need the drive letter; we have no use for the colon that follows the drive letter.

Now, as we noted earlier, we can’t write a string value (like D) to the errorlevel environment variable. Therefore, we use the VBScript Asc function to convert D to its ASCII (numeric) equivalent:

intDrive = Asc(strDrive) – 64

Good question: why do we subtract 64 from the ASCII value? Admittedly, we don’t have to. However, the ASCII value of the letter A is 65; if we subtract 64 from 65 we get 1. That means, in our script, the value of A ends up being 1; coincidentally enough, A also happens to be letter number 1 in the English alphabet. The ASCII value of D is 68. If we subtract 64 from 68 we get 4, and D just happens to be letter number – right; you got it.

From there all we have to do is call the Quit method to terminate the script, making sure to pass our “drive letter” (or at least its numeric equivalent) as the sole parameter to the Quit method:


What does all that gain us? Well, suppose we run our script, and then run the following little batch file:

echo off
echo %errorlevel%

The batch file will echo back the value 4, which happens to be the very same value we derived from our script. Neat, huh? All you have to do now is modify the batch file so that it converts the value 4 back to drive letter D. But that’s something you’ll have to do on your own. After all, we Scripting Guys can’t do everything for you.

And would it hurt you to call once in awhile? And maybe get a haircut? And just how long do you plan to spend at the bowling alley before you start looking for a real job? After all the money it cost to send you to college and get you that fancy degree, you end up …


Discussion is closed.

Feedback usabilla icon