Skip to content

Dear Internet Explorer user: Your browser is no longer supported

Please switch to a modern browser such as Microsoft Edge, Mozilla Firefox or Google Chrome to view this website's content.

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!

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.