February 7th, 2006

How Can I Determine the Drive Letter for the Drive My Script is Currently Running On?

Hey, Scripting Guy! Question

Hey, Scripting Guy! I have a script that runs off a CD and copies a bunch of files to a computer. However, in order to copy the files I need to know the complete file path, including the drive letter. How can I determine the drive letter for the drive my script is currently running on?

— RKG

SpacerHey, Scripting Guy! AnswerScript Center

Hey, RKG. You know, about a year or so ago one of the Scripting Guys installed – or, more accurately, tried to install – a relatively well-known application.

Note. If this had been a Microsoft program we’d tell you the name, but this wasn’t a Microsoft program. As if Microsoft would ever release software that didn’t work flawlessly right from the start!

As you probably already guessed, this application wouldn’t install. Why not? Because the setup program assumed that the CD-ROM drive would always be drive D. When faced with a computer where the CD drive was on drive E, the installation program did the only thing it could do: it gave up. In order to install the application you had to download a patch that could deal with alternate drive letters.

Of course, even then you were prompted to enter the drive letter to your CD-ROM; the patch couldn’t figure out the drive letter for the CD drive, either. But at least you could finally install the software.

Pretty sad story, isn’t it? And what makes it sadder is that this multi-million dollar software company could have solved their problem using a script no more complicated than this:

Set objFSO = CreateObject(“Scripting.FileSystemObject”)
Set objShell = CreateObject(“Wscript.Shell”)

strPath = objShell.CurrentDirectory strDrive = objFSO.GetDriveName(strPath)

Wscript.Echo strDrive

Good point: we should have contacted this company and offered to act as high-priced installation consultants. Now you tell us!

As you can see, this is a pretty simple little script. We begin by creating instances of two objects: Scripting.FileSystemObject and Wscript.Shell. We then encounter this line of code:

strPath = objShell.CurrentDirectory

It goes without saying (although we’ll say it anyway) that the CurrentDirectory property reports back the current directory for the script. For example, suppose our CD-ROM is drive E, and the script is running in the Setup folder. Try to guess the value of the CurrentDirectory property:

E:\Setup

You’re right; that was a bit too easy, wasn’t it?

Once we have a file path we can then use the FileSystemObject’s GetDriveName method to extract the drive letter from that path. That’s what we do here:

strDrive = objFSO.GetDriveName(strPath)

We simply pass GetDriveName the file path in question, then store the returned value in the variable strDrive. And what do you suppose we get when we echo back the value of strDrive? You got it:

E:

All of which means that now you’re ready to act as a high-priced installation consultant. When you get your first big job, remember: it was the Scripting Guys who helped you get started. And, yes, we will take a check. Thanks for asking. (Editor’s Note: Time for the editor to make the lawyers happy: As much as we’d like to accept your check [money order, credit card number, cash, etc.], this column and all advice in it are provided free of charge. The Scripting Guys will have to continue to survive on their regular paychecks.)

Author

0 comments

Discussion are closed.