How Can I Change the Working Folder of a Script?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! My script needs to have the same working folder as the application that the script starts. How can I change the working folder of a script?

— JM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JM. You can change the current (or working) folder of a script simply by setting the value of the Wscript Shell object’s CurrentDirectory property. (Note. The CurrentDirectory property was introduced in Windows Script Host 5.6, so the scripts we’ll show you today require you to be running that version of WSH.) For example, here’s a demonstration script that echoes the current directory for a script, changes the current directory to C:\Windows, and then echoes the current directory again:

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

objShell.CurrentDirectory = “C:\Windows” Wscript.Echo objShell.CurrentDirectory

As you might expect, in the last line of the script C:\Windows is echoed as the name of the current directory.

Now that we know how to change the current folder all we have to do is write a simple little script that starts an application and then changes the current directory to match the working folder of that application. For example, this script starts Notepad (C:\Windows\System32\Notepad.exe), determines the desired working folder by parsing the path to Notepad.exe, and then changes the current directory to C:\Windows\System32:

Set objShell = CreateObject(“Wscript.Shell”)

strApp = “C:\Windows\System32\Notepad.exe” arrPath = Split(strApp, “\”)

For i = 0 to Ubound(arrPath) – 1 strAppPath = strAppPath & arrPath(i) & “\” Next

objShell.CurrentDirectory = strAppPath Wscript.Echo objShell.CurrentDirectory

objShell.Run(strApp)

All we’re doing is storing the application path (C:\Windows\System32\Notepad.exe) in a variable named strApp. We then use the VBScript Split function to split this path into an array (using the \ as our delimiter); that gives us an array consisting of these elements:

C:
Windows
System32
Notepad.exe

Why do we want an array consisting of these elements? Well, as you can see, the first three elements – plus a couple \’s – give us the path we want: C:\Windows\System32. By splitting the path like this, we can determine the folder path by taking the first three items and discarding the executable file name (Notepad.exe), That’s what we do with this line of code, which reconstructs the path using all the items in the array except the very last one:

For i = 0 to Ubound(arrPath) – 1
    strAppPath = strAppPath & arrPath(i) & “\”
Next

In case you’re wondering, arrays always start with item 0, which is why our For-Next loop starts at 0. In turn, the Ubound property represents the last item in the array. We don’t want to use the last item, we only want to go as far as the next-to-last item. Hence Ubound – 1, which simply means the last item in the array minus 1, or the next-to-last item. In the sample script shown here, that gives us this equation:

C: + \ + Windows + \ + System32 + \

Or, C:\Windows\System32\. (The extra \ on the end doesn’t make any difference, so we don’t bother trimming it off.)

From there we change the current directory to C:\Windows\System32; echo the value of the current directory (just so you know that the change has been made); and then go ahead and start Notepad. Problem solved!

0 comments

Discussion is closed.

Feedback usabilla icon