FacebooktwitterredditlinkedinmailFacebooktwitterredditlinkedinmail

This post shows how to quickly get the current script directory using PowerShell, VBScript and Batch – the most commonly used scripting languages for Windows.

The scripts I write usually read in other files or call other scripts. In order for these scripts to run from any location – such as a UNC path – without hard coding paths they need to use relative paths. Depending on how the script is called the working directory may not be the same as the script file. For example, if your current directory is C:\Windows and you run the script \\server\share\somepath\myscript.cmd then any relative paths in the script file won’t work correctly.

One way around this is to make the script change working directory right at the start and then use relative paths after that. But in some situations – such as batch files on UNC paths – this won’t aways work. The best way to get around this is to determine the directory that the script resides in at the start of the script and then make all other paths reference that.

Because I jump around various scripting languages all the time, I tend to forget the best way to do this and have to hunt for examples in old scripts. As a reference for myself this post gives the template for getting the current script directory in the languages I tend to use: PowerShell, VBScript and batch.

 

Windows Batch

Windows batch is the trickiest in some ways – it also is the one that cannot support UNC working directories. There is a built-in variable %~dp0 which expands to the path that the script is located in including the trailing slash. This can make for messy looking scripts because to run setup.exe from the current script directory you would use %~dp0setup.exe. This works great but can be a little confusing for others to read because it looks like a typo.

My preferred method is to create a new variable at the top of the script using %~dp0 and then stripping the trailing backslash. Here is the script:

@ECHO OFF
REM Determine script location for Windows Batch File

REM Get current folder with no trailing slash
SET ScriptDir=%~dp0
SET ScriptDir=%ScriptDir:~0,-1%
 
ECHO Current script directory is %ScriptDir%
@ECHO OFF
REM Determine script location for Windows Batch File

REM Get current folder with no trailing slash
SET ScriptDir=%~dp0
SET ScriptDir=%ScriptDir:~0,-1%

ECHO Current script directory is %ScriptDir%

 

VBScript

VBScript is fairly straightforward the full path of the running script is available in WScript.ScriptFullName and then you can use the FileSystemObject class to get the parent folder name. Here is the script:

' Determine script location for VBScript
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)
 
Wscript.Echo "Current script directory is " & sScriptDir
' Determine script location for VBScript
Dim oFSO
Set oFSO = CreateObject("Scripting.FileSystemObject")
sScriptDir = oFSO.GetParentFolderName(WScript.ScriptFullName)

Wscript.Echo "Current script directory is " & sScriptDir

 

PowerShell

PowerShell users have long used a snippet from this post that gets the script folder. However, it doesn’t work as expected depending on how the script was loaded. This altered version should work in all cases:

# Determine script location for PowerShell
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path
 
Write-Host "Current script directory is $ScriptDir"
# Determine script location for PowerShell
$ScriptDir = Split-Path $script:MyInvocation.MyCommand.Path

Write-Host "Current script directory is $ScriptDir"

 

Hopefully this post will be a useful reference to you when trying to remember how to get the current script directory. I know that I’ll end up referencing it myself in the future!

 

FacebooktwitterredditlinkedinmailFacebooktwitterredditlinkedinmail