MYHOSPITALNOW.COM

Find Trusted Doctors & Hospitals

Discover healthcare providers, compare hospitals, and explore treatment options worldwide.

Free Treatment Quotes • Doctor Search • Hospital Comparison

Find Healthcare Options

www.myhospitalnow.com

%~dp0 VS %cd%

%~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.

rajeshkumar created the topic: %~dp0 VS %cd% Use of %~dp0 and Use of %cd% They are not equivalent. %cd% is available either to a batch file or at the command prompt and expands to the drive letter and path of the current directory (which can change e.g. by using the CD command) %~dp0 is only available within a batch file and expands to the drive letter and path in which that batch file is located (which cannot change). It is obtained from %0 which is the batch file’s name. An experiment like the following shows the difference Here is D:\dirshow.bat: @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
ReturnsCurrent working directoryDrive and folder of the batch file
Works inBatch files and the interactive promptBatch files only (printed literally at the prompt)
Changes whenYou run cd, pushd or start the script from elsewhereNever changes during the script
Trailing backslashOnly at a drive root, such as C:\Always, such as D:\scripts\
Use it forPaths relative to where the user is workingFiles 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:

ModifierResult for D:\scripts\dirshow.bat
%~0The name as typed, with surrounding quotes removed
%~f0D:\scripts\dirshow.bat (full path)
%~d0D:
%~p0\scripts\
%~dp0D:\scripts\
%~n0dirshow
%~x0.bat
%~nx0dirshow.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\System32 as 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.
  • %~dp0 already ends with a backslash, so write "%~dp0config.ini", not "%~dp0\config.ini".
  • pushd also works when the script is on a network share (UNC path such as \\server\share): it maps a temporary drive letter. cd /d cannot make a UNC path the current directory.
  • Save %~dp0 into a variable at the top. A plain shift command also shifts %0, after which %~dp0 no longer points to the script.

Equivalents in PowerShell and Bash

Script folderCurrent directory
Batch%~dp0%cd%
PowerShell$PSScriptRootGet-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

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
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.

Related Posts

Windows XP/2000 Commands and Tools: Practical Admin Reference with Examples

A modernized, task-oriented reference to key Windows XP/2000 command-line tools for troubleshooting, system administration, and automation. Includes command comparisons, workflow guidance, and copy-ready examples.

Read More

Run Remote Desktop from the Command Line with mstsc.exe (Including Legacy Console Mode)

Learn how to launch Remote Desktop sessions from Command Prompt or batch files using mstsc.exe. This updated guide covers syntax, common command patterns, and legacy console-session behavior.

Read More

Windows Batch Scripts to Shut Down or Restart a PC (Legacy and Modern Methods)

Learn how to build simple Windows batch files for restart and shutdown operations. This revision preserves the original legacy SHUTDOWN.EXE approach and adds modern, built-in shutdown command examples.

Read More

MS-DOS Basics: Practical Commands for Files, Directories, and Legacy Disk Tasks

This revised guide turns a classic DOS tutorial into a structured, practical reference for command-line basics. Learn essential MS-DOS commands for navigation, file operations, directory management, cleanup, and legacy floppy workflows.

Read More

Windows Batch Script for Perforce Login and Sync (Updated Step-by-Step Guide)

This guide shows how to automate Perforce login and forced sync from a Windows batch file, based on the original 2009 post. It also covers safer authentication patterns, validation commands, and common failure points.

Read More

Batch Script to Login & Sync Source Code from Perforce Server to a Specific Location

This script will:โœ… Login to the Perforce serverโœ… Sync the latest source code to a specific local directoryโœ… Automatically handle errors and logging 1. Prerequisites Before running…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
0
Would love your thoughts, please comment.x
()
x