Programming
Scripting Language
Code Explanation
Command Line Tools
Batch Files

What does %~dp0 mean, and how does it work?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

%~dp0 is a Windows batch file variable that expands to the drive letter and directory path of the currently running script. If your batch file lives at C:\Projects\deploy\run.bat, then %~dp0 evaluates to C:\Projects\deploy\. It always ends with a trailing backslash. This is the single most important variable for writing portable batch scripts that reference files relative to their own location.

Breaking Down the Syntax

The expression %~dp0 is built from three parts:

PartMeaningExample
%0The batch file itself (argument zero)C:\Projects\deploy\run.bat
~dExtract the drive letterC:
~pExtract the path (without drive, without filename)\Projects\deploy\

Combined, ~dp gives you drive + path. The 0 refers to argument zero, which is always the script itself.

All available modifiers

Windows batch files support a full set of modifiers on any argument %0 through %9:

ModifierExpands toExample (for C:\Projects\deploy\run.bat)
%~0Full path with quotes removedC:\Projects\deploy\run.bat
%~f0Fully qualified pathC:\Projects\deploy\run.bat
%~d0Drive letter onlyC:
%~p0Path only (no drive, no filename)\Projects\deploy\
%~n0Filename without extensionrun
%~x0Extension only.bat
%~dp0Drive + pathC:\Projects\deploy\
%~nx0Filename + extensionrun.bat
%~dpnx0Full path (same as %~f0)C:\Projects\deploy\run.bat
%~z0File size in bytes1024
%~t0Date/time of file06/15/2026 02:30 PM
%~a0File attributes--a------
%~s0Short (8.3) pathC:\PROJEC~1\DEPLOY~1\run.bat

You can look up this full list anytime by running call /? in a CMD window.

Practical Examples

Example 1: Run an executable next to the batch file

batch
@echo off
"%~dp0myapp.exe" --config "%~dp0config.ini"
pause

This launches myapp.exe from the same folder as the batch file, passing a config file also in that folder. It works regardless of what directory the user runs the script from.

Example 2: Set the working directory to the script location

batch
@echo off
cd /d "%~dp0"
echo Current directory: %CD%

The /d flag is important. Without it, cd cannot switch drives (e.g., from D: to C:).

Example 3: Reference a subfolder

batch
1@echo off
2set TOOLS_DIR=%~dp0tools\
3set LOGS_DIR=%~dp0logs\
4
5echo Tools: %TOOLS_DIR%
6echo Logs: %LOGS_DIR%
7
8"%TOOLS_DIR%compiler.exe" --output "%LOGS_DIR%build.log"

Since %~dp0 always ends with \, appending tools\ gives you C:\Projects\deploy\tools\.

Example 4: Add the script directory to PATH temporarily

batch
@echo off
set PATH=%~dp0bin;%PATH%
python myscript.py

This prepends the bin subfolder to PATH for the duration of the script, making any executables in that folder available.

Example 5: Pass the script directory to a PowerShell command

batch
@echo off
powershell -ExecutionPolicy Bypass -File "%~dp0setup.ps1" -BasePath "%~dp0"

Example 6: Log the script location for debugging

batch
1@echo off
2echo Script: %~f0
3echo Directory: %~dp0
4echo Drive: %~d0
5echo Filename: %~nx0
6echo Size: %~z0 bytes
7echo Modified: %~t0

Output:

 
1Script: C:\Projects\deploy\run.bat
2Directory: C:\Projects\deploy\
3Drive: C:
4Filename: run.bat
5Size: 256 bytes
6Modified: 06/15/2026 02:30 PM

%~dp0 vs. %CD% vs. pushd/popd

These three approaches serve different purposes:

Variable/CommandReturnsChanges when user cds?
%~dp0Directory of the batch fileNo (always the script location)
%CD%Current working directoryYes
pushd "%~dp0"Sets CWD to script dir, saves previousYes (restores with popd)

%CD% is where the user was when they ran the script. %~dp0 is where the script lives. They are often different:

batch
@echo off
echo User ran me from: %CD%
echo But I live in: %~dp0

If the user types C:\Users\alice> D:\scripts\deploy.bat, then %CD% is C:\Users\alice and %~dp0 is D:\scripts\.

Using pushd/popd for temporary directory changes

batch
1@echo off
2pushd "%~dp0"
3rem Now CWD is the script directory
4call build.bat
5call test.bat
6popd
7rem CWD is restored to where the user was

%~dp0 Inside Subroutines (CALL)

When you use CALL :label, %~dp0 still refers to the main script, not the subroutine. However, inside a CALLed external script, %~dp0 refers to that external script's location:

batch
1@echo off
2rem main.bat (in C:\Projects\)
3echo Main script dir: %~dp0
4
5call "%~dp0lib\helper.bat"
batch
@echo off
rem lib\helper.bat (in C:\Projects\lib\)
echo Helper script dir: %~dp0

Output:

 
Main script dir: C:\Projects\
Helper script dir: C:\Projects\lib\

Quoting Rules

Always wrap %~dp0 in double quotes when using it in paths. Without quotes, paths containing spaces will break:

batch
1rem BAD: breaks if path contains spaces
2cd /d %~dp0
3
4rem GOOD: handles spaces correctly
5cd /d "%~dp0"
6
7rem BAD: breaks on "C:\My Projects\deploy\app.exe"
8%~dp0app.exe
9
10rem GOOD: handles spaces
11"%~dp0app.exe"

The Equivalent in PowerShell

PowerShell has its own variables for the same purpose:

BatchPowerShellDescription
%~dp0$PSScriptRootDirectory of the script
%~f0$PSCommandPathFull path of the script
%~nx0Split-Path $PSCommandPath -LeafFilename of the script
powershell
# PowerShell equivalent of %~dp0
$scriptDir = $PSScriptRoot
$configPath = Join-Path $scriptDir "config.json"

In Bash on Linux/macOS, the equivalent is:

bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"

Common Pitfalls

  • Forgetting /d with cd. cd %~dp0 does not change drives. If the script is on D: and the user is on C:, the cd will silently fail. Always use cd /d "%~dp0".
  • Double backslash when appending. Since %~dp0 ends with \, writing %~dp0\subfolder produces C:\Projects\\subfolder. This usually works in Windows but looks wrong. Omit the extra backslash: %~dp0subfolder.
  • Using %~dp0 inside FOR loops. Inside a FOR loop, %0 may be rebound. If you need the script directory inside a loop, save it to a variable first: set SCRIPT_DIR=%~dp0.
  • UNC paths. If the script is on a network share (\\server\share\script.bat), cd /d "%~dp0" will fail because cd does not support UNC paths. Use pushd "%~dp0" instead, which maps a temporary drive letter.
  • Delayed expansion confusion. If you use enabledelayedexpansion, make sure you are not mixing ! and % syntax when referencing %~dp0. The %~dp0 variable is expanded at parse time, not at execution time, so it always uses %.

Summary

  • %~dp0 returns the drive and directory path of the batch file that is currently running, always ending with a backslash.
  • %0 is the script itself, ~d extracts the drive, ~p extracts the path.
  • Always quote it: "%~dp0" to handle spaces in paths.
  • Use cd /d "%~dp0" to change the working directory to the script location (the /d flag enables cross-drive changes).
  • Use pushd "%~dp0" for UNC network paths or when you want to restore the original directory later with popd.
  • The PowerShell equivalent is $PSScriptRoot. The Bash equivalent is $(cd "$(dirname "$0")" && pwd).
  • Run call /? in CMD to see the full list of available modifiers.

Course illustration
Course illustration

All Rights Reserved.