Skip to content

Relative file paths in BAT files

G30th January 2019

CNo Comments

TPowerShell, Windows

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.



No comments have yet been submitted. Be the first!

Have your say:

The following HTML is permitted:
<a href="" title=""> <b> <blockquote cite=""> <code> <em> <i> <q cite=""> <strike> <strong>

Comments will be published subject to the Editorial Policy.