What does it mean by command cd /d %~dp0 in Windows

WindowsCmd

Windows Problem Overview


Can someone please help me to understand the command cd /d %~dp0 and its purposes. Again dos command is below

cd /d %~dp0

Please help me to get the meaning of it.

Windows Solutions


Solution 1 - Windows

Let's dissect it. There are three parts:

  1. cd -- This is change directory command.
  2. /d -- This switch makes cd change both drive and directory at once. Without it you would have to do cd %~d0 & cd %~p0. (%~d0 Changs active drive, cd %~p0 change the directory).
  3. %~dp0 -- This can be dissected further into three parts:
    1. %0 -- This represents zeroth parameter of your batch script. It expands into the name of the batch file itself.
    2. %~0 -- The ~ there strips double quotes (") around the expanded argument.
    3. %dp0 -- The d and p there are modifiers of the expansion. The d forces addition of a drive letter and the p adds full path.

Solution 2 - Windows

~dp0 : d=drive, p=path, %0=full path\name of this batch-file.

cd /d %~dp0 will change the path to the same, where the batch file resides.

See for /? or call /? for more details about the %~... modifiers.
See cd /? about the /d switch.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionIndranil SarkarView Question on Stackoverflow
Solution 1 - WindowswilxView Answer on Stackoverflow
Solution 2 - WindowsStephanView Answer on Stackoverflow