Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

windows

cmd

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.

like image 434
Indranil Sarkar Avatar asked Aug 19 '13 08:08

Indranil Sarkar


People also ask

What does %~ dp0 mean in batch file?

The difference between %CD% and %~dp0 Second, for %CD%, the current directory means the directory when executing the command line or the batch file. For %~dp0, the current directory is the directory where the bat file resides. So if you put the batch file in c:\dir\test.

What does %~ mean in a batch file?

The %* modifier is a unique modifier that represents all arguments passed in a batch file. You cannot use this modifier in combination with the %~ modifier. The %~ syntax must be terminated by a valid argument value. Source: "Using batch parameters" on Microsoft.com (defunct)

What does cd mean in batch?

The cd command, also known as chdir (change directory), is a command-line shell command used to change the current working directory in various operating systems. It can be used in shell scripts and batch files.

What does %% do in Windows?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.


1 Answers

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.
like image 81
wilx Avatar answered Oct 02 '22 21:10

wilx