How Can I Determine the Path to the Folder Where a Script is Running?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine the path to the folder where a script is running? What I’d like to do is have the script automatically open the folder where the script is running.

— PD

SpacerHey, Scripting Guy! AnswerScript Center

Hey, PD. You know, for the Scripting Guys this question – as do so many of the questions we receive – falls under the category Why Didn’t We Think of That? The Scripting Guys spend a lot of time testing scripts, including scripts submitted by members of the Windows scripting community. (Well, one of the Scripting Guys spends a lot of time testing scripts. Not that he’s complaining, mind you, even though he has to do pretty much all the work around here. [Editor’s Note: Including most of the whining.]) Many of these scripts add files to or delete files from a folder or write information to a log file in the same folder where the script itself can be found. That means we often need to have Windows Explorer opened to the script’s home folder. How do we open that folder? Well, we double-click My Computer, we double-click C:, we double-click Scripts, etc., etc. It never occurred to us to have the script go ahead and open up the folder for us.

You know, someone really should write a daily column explaining how to do useful little scripting tasks like that. That would be a handy thing to have, wouldn’t it?

So maybe the Scripting Guys aren’t particularly good at thinking up new ideas. However, once someone else has come up with an idea we can usually help them turn that idea into a script:

Set objShell = CreateObject(“Wscript.Shell”)

strPath = Wscript.ScriptFullName

Set objFSO = CreateObject(“Scripting.FileSystemObject”)

Set objFile = objFSO.GetFile(strPath) strFolder = objFSO.GetParentFolderName(objFile)

strPath = “explorer.exe /e,” & strFolder objShell.Run strPath

As you can see, this isn’t a particularly complicated script. We begin by creating an instance of the Wscript.Shell object; we’ll use this object later to launch Windows Explorer. We then use this line of code to get the full path to the running script (for example, C:\Scripts\My_script.vbs):

strPath = Wscript.ScriptFullName

Before we go any further, a quick clarification. Some of you might be wondering why we didn’t use the CurrentDirectory property to determine the current directory of the script. Well, we could have, except for one thing: the current directory of the script isn’t necessarily the same folder in which the script is running. After all, you can easily start a script in C:\Scripts and then change the current directory to C:\Test. For this column we wanted the name of the folder where the script resides. If you’d prefer to get the current directory then use this script instead:

Set objShell = CreateObject(“Wscript.Shell”)
strPath = objShell.CurrentDirectory

strPath = “explorer.exe /e,” & strPath objShell.Run strPath

Now, back to our original script. After storing the full path to the script in the variable strPath we next create an instance of the Scripting.FileSystemObject. That brings us to these two lines of code:

Set objFile = objFSO.GetFile(strPath)
strFolder = objFSO.GetParentFolderName(objFile)

At the moment we have the path to the script file itself: C:\Scripts\My_script.vbs. That’s fine, but what we really want to do is get rid of the file name and leave us with just the folder path: C:\Scripts. Although there are several different ways we could do this, we felt this approach was the simplest. What we do here is use the GetFile method to bind to the file C:\Scripts\My_script.vbs (notice how we pass the variable strPath as the sole parameter to GetFile). After making the connection to the file we can then use the GetParentFolderName method to get the full path to the folder where the file resides; needless to say, that happens to be C:\Scripts. And that also happens to be the very folder we want to open.

The rest is easy. With the path to the script folder safely tucked away in a variable named strPath, we can then use this line of code to construct a command line string that will open a Windows Explorer window, a window that has the focus set to C:\Scripts:

strPath = “explorer.exe /e,” & strPath

All that’s left to do now is call the Run method, passing along the command line string we just constructed:

objShell.Run strPath

Cool, huh? Now, anyone out there have an idea for tomorrow’s column?

0 comments

Discussion is closed.

Feedback usabilla icon