%~dp0 is the folder that contains the running batch file, while %cd% is the current working directory of the command prompt. They are the same only when you start the script from its own folder. If your script needs files stored next to it, use %~dp0.
@echo off
echo this is %%cd%% %cd%
echo this is %%~dp0 %~dp0
Run it from C:\ and this is what you see
C:\>D:\dirshow.bat
this is %cd% C:\
this is %~dp0 D:\
Regards,
Rajesh Kumar
Twitt me @ twitter.com/RajeshKumarIn
Quick comparison
| %cd% | %~dp0 | |
|---|---|---|
| Returns | Current working directory | Drive and folder of the batch file |
| Works in | Batch files and the interactive prompt | Batch files only (printed literally at the prompt) |
| Changes when | You run cd, pushd or start the script from elsewhere | Never changes during the script |
| Trailing backslash | Only at a drive root, such as C:\ | Always, such as D:\scripts\ |
| Use it for | Paths relative to where the user is working | Files shipped next to the script |
How %~dp0 is built
%0 is the name used to start the batch file. The ~ modifiers extract parts of it, and you can combine them:
| Modifier | Result for D:\scripts\dirshow.bat |
|---|---|
%~0 | The name as typed, with surrounding quotes removed |
%~f0 | D:\scripts\dirshow.bat (full path) |
%~d0 | D: |
%~p0 | \scripts\ |
%~dp0 | D:\scripts\ |
%~n0 | dirshow |
%~x0 | .bat |
%~nx0 | dirshow.bat |
The same modifiers work on arguments (%~dp1 is the folder of the first argument) and on for loop variables (%%~dpF).
Why %cd% is unreliable in real scripts
- Run as administrator: the current directory becomes
C:\Windows\System32, so%cd%points there instead of your script folder. - Task Scheduler: unless you fill in “Start in”, tasks start in
C:\Windows\System32as well. - Called from another script or a CI agent:
%cd%is the caller’s working directory. - Double-clicking in Explorer starts in the script folder, which is why a script can work on your machine and fail elsewhere.
Recommended pattern
@echo off
setlocal
rem Remember the script folder before anything can change %0
set "SCRIPT_DIR=%~dp0"
rem Work relative to the script folder, then return to where we started
pushd "%SCRIPT_DIR%"
echo Script folder : %SCRIPT_DIR%
echo Working folder: %cd%
if exist "%SCRIPT_DIR%config.ini" echo Found config.ini next to the script
popd
endlocalCode language: PHP (php)
- Always quote paths:
"%~dp0". Without quotes the script breaks when the folder name contains spaces. %~dp0already ends with a backslash, so write"%~dp0config.ini", not"%~dp0\config.ini".pushdalso works when the script is on a network share (UNC path such as\\server\share): it maps a temporary drive letter.cd /dcannot make a UNC path the current directory.- Save
%~dp0into a variable at the top. A plainshiftcommand also shifts%0, after which%~dp0no longer points to the script.
Equivalents in PowerShell and Bash
| Script folder | Current directory | |
|---|---|---|
| Batch | %~dp0 | %cd% |
| PowerShell | $PSScriptRoot | Get-Location or $PWD |
| Bash | "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" | $PWD or pwd |
Frequently asked questions
Does %~dp0 work in .cmd files?
Yes. .bat and .cmd files are both run by cmd.exe and support the same modifiers.
Why does %~dp0 show the literal text at the command prompt?
Because %0 only exists inside a running batch file. At the interactive prompt there is no script, so the text is not expanded.
How do I get the folder without the trailing backslash?
Store it and cut the last character: set "DIR=%~dp0" followed by set "DIR=%DIR:~0,-1%".
Related tutorials
I’m Rajesh Kumar, a DevOps, SRE, DevSecOps, Cloud, and Platform Engineering expert passionate about sharing practical knowledge, real-world experiences, and industry best practices. I have worked at Cotocus and regularly write about technology, travel, investing, health, product reviews, and digital marketing through my various platforms.
I publish technical articles at DevOps School, travel stories at Holiday Landmark, stock market insights at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at TrueReviewNow, and SEO and digital marketing strategies at Wizbrand.
Find Trusted Cardiac Hospitals
Compare heart hospitals by city and services โ all in one place.
Explore Hospitals