%~dp0 is not a Windows command, environment variable, or Settings option. It is a cmd.exe batch-parameter expansion that resolves to the drive and directory path of the currently running batch file.
For example, if a script is stored at C:ToolsInstallinstall.cmd, this line:
echo Script directory: "%~dp0"
normally prints:
Script directory: "C:ToolsInstall"
The trailing backslash is included, which makes %~dp0 useful for addressing files stored beside a batch script.
What each part of %~dp0 means
The notation is built from a batch parameter and modifiers:
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
| Part | Meaning |
|---|---|
%0 |
The batch file’s invocation parameter—the script currently being executed. |
~ |
Applies parameter modifiers, including removing surrounding quotation marks. |
d |
Returns the drive letter. |
p |
Returns the path. |
Therefore, %~dp0 means “the drive and path of parameter zero.” In normal batch-file execution, parameter zero is the batch file itself. Microsoft documents the equivalent general form %~dp1 as the drive letter and path only; replacing %1 with %0 produces %~dp0.
The dp0 portion is not an environment-variable name. The parameter number, 0, terminates the expansion.
Basic example
Create a file named show-location.cmd with this content:
@echo off
echo Script directory: "%~dp0"
pause
Save it in C:ScriptsDemo and run it. The output should identify C:ScriptsDemo, even if you launched the file from another directory.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →There is no DP0 command to type at a normal Command Prompt, and there is no related Windows menu or button. The syntax is meaningful in a batch-file context, where %0 through %9 are batch parameters.
Using it to locate files beside the script
The most common use is building a path to a file distributed with the script:
@echo off
"%~dp0My Tool.exe"
rem Or, for a configuration file:
type "%~dp0dataconfig.ini"
If the script is in C:Program FilesAcme Tool, quoting is essential. This is safe:
"%~dp0My Tool.exe"
This is unsafe:
%~dp0My Tool.exe
Without quotation marks, cmd.exe can split the path at the space and interpret C:Program as one token and the remaining text as other arguments. Quote the complete path, not just the filename.
Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Clear out junk files and repair common Windows errors3Fix the driver behind crashes, sound loss and screen glitches%~dp0 versus %CD%
These values can be different:
%~dp0is derived from the batch file’s location.%CD%is the shell’s current working directory.
Suppose C:Scriptsbackup.cmd is started while the current directory is D:Work. The script can see:
%~dp0 = C:Scripts
%CD% = D:Work
A relative path such as dataconfig.ini normally resolves from the current working directory, not automatically from the script’s directory. To deliberately use the script’s location, write:
"%~dp0dataconfig.ini"
This distinction matters when a script is launched through a shortcut, scheduled task, another script, or an installer. Relying on the current directory can work interactively and then fail when the same script is automated.
Changing the working directory
If all subsequent relative paths should resolve beside the batch file, change directory explicitly:
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minute@echo off
cd /d "%~dp0"
rem Relative paths now start in the script directory
mytool.exe dataconfig.ini
The /d switch allows cd to change drives as well as directories. For example, a script on E: can move to its own directory even when the command session currently uses C:.
This changes the current directory; it does not change what %~dp0 represents. The expansion remains tied to the batch file’s parameter zero.
Saving the directory in a variable
Use this form when the script refers to its directory repeatedly:
set "SCRIPT_DIR=%~dp0"
echo "%SCRIPT_DIR%"
"%SCRIPT_DIR%toolshelper.exe"
The quotation marks around the entire set assignment protect spaces and special characters without becoming part of the variable’s value. Avoid this form:
set SCRIPT_DIR="%~dp0"
That assigns the quotation marks as part of SCRIPT_DIR, which can cause confusing results when the variable is quoted again later.
Related batch-parameter expansions
These variants are useful when you need a different part of the script’s path:
Rank #4
| Expansion | Result |
|---|---|
%~f0 |
Fully qualified path, including the script filename. |
%~d0 |
Drive only. |
%~p0 |
Path only. |
%~n0 |
Filename without its extension. |
%~x0 |
Filename extension. |
%~nx0 |
Filename and extension. |
For example, if the running file is C:Toolsinstall.cmd:
%~f0 = C:Toolsinstall.cmd
%~d0 = C:
%~p0 = Tools
%~n0 = install
%~x0 = .cmd
%~nx0 = install.cmd
Use %~f0 when you need to pass the batch file itself to another command. Use %~dp0 when you need the directory only.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
Nested batch files
When one batch file invokes another with call, each file has its own parameter-zero context:
rem parent.cmd
@echo off
call "%~dp0child.cmd"
Inside child.cmd, %~dp0 points to the directory containing child.cmd, not automatically to the parent script’s directory. This makes each called script able to locate its own companion files.
Delayed expansion and parentheses
Delayed expansion uses exclamation-mark syntax, such as !VAR!, and is enabled with setlocal EnableDelayedExpansion or cmd /v:on. It does not control %~dp0, which is a percent-based batch-parameter expansion.
Batch files do have parse-time behavior for percent substitutions, especially inside parenthesized blocks. That can affect ordinary variables assigned inside a block. It does not change the meaning of %~dp0: the expansion still represents the batch file associated with parameter zero. A later cd also does not turn it into %CD%.
Best Value
Limitations and failure modes
- Command extensions disabled: parameter-modifier syntax depends on command extensions. A process started with
cmd /e:offmay not support the expected expansion. Scripts that depend on%~dp0should not assume extensions have been disabled. - It does not include the filename:
%~dp0ends at the directory. Use%~f0or append a filename yourself. - It is not a path-normalization command: it supplies the drive and path derived from the batch parameter. It does not prove that a child file exists or perform general validation. Check separately with
if existwhen needed. - It is not the current directory: use
%CD%when you specifically need the shell’s current working directory. - It is not for
%*: the documented modifiers apply to individual batch parameters such as%0through%9, not to the all-arguments substitution%*.
Practical template
This template keeps companion files anchored to the script and reports a useful error if one is missing:
@echo off
setlocal
set "SCRIPT_DIR=%~dp0"
set "CONFIG=%SCRIPT_DIR%dataconfig.ini"
if not exist "%CONFIG%" (
echo Missing configuration: "%CONFIG%"
exit /b 1
)
mytool.exe "%CONFIG%"
endlocal
For Microsoft’s parameter-modifier syntax, see the Windows call command documentation. Microsoft’s cmd documentation covers command extensions and quotation behavior.
FAQ
Is DP0 a Windows command?
No. The correct notation is %~dp0. It is a cmd.exe batch-parameter expansion, not a standalone command, setting, or environment variable.
What does %~dp0 return?
It returns the drive and directory path associated with the currently executing batch file, normally including a trailing backslash.
Does %~dp0 mean the current directory?
No. %~dp0 identifies the script directory, while %CD% identifies the current working directory. They can differ.
Why should I put quotes around %~dp0?
A script directory may contain spaces, such as C:Program Files. Quoting the complete path prevents cmd.exe from splitting it into separate tokens.
How do I make the script directory the current directory?
Run cd /d "%~dp0". The /d switch also changes the drive when necessary.
How do I get the batch file’s complete path?
Use %~f0. Unlike %~dp0, it includes the script filename and extension.
The Bottom Line
%~dp0 means “the drive and path of the running batch file.” Use it when a script must reliably find files stored beside itself, and quote the resulting path: "%~dp0filename.ext". Remember that it is different from %CD%, and use cd /d "%~dp0" only when you intentionally want to change the working directory.

