Relative file paths in BAT files
Instructions for using a relative file path in batch files.
To insert a relative path in a batch file, use the %~dp0
command.
For instance, in writing a batch file to initiate a PowerShell script, a relative path can be used so that the file will work in any directory that it is placed:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File ""%~dp0%script.ps1""' -Verb RunAs}"
The trailing percent sign (%
) prevents the double trailing slash that would occur if %~dp0\script.ps1
were used instead.
The %~dp0
command will work with mapped network drives, local drives and UNC file paths.
Comments
No comments have yet been submitted. Be the first!