Windows 中的命令 cd /d %~dp0 是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18309941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:47:42  来源:igfitidea点击:

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

windowscmd

提问by Indranil Sarkar

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

有人可以帮助我理解命令cd /d %~dp0及其目的。再次 dos 命令如下

cd /d %~dp0

Please help me to get the meaning of it.

请帮助我理解它的含义。

回答by wilx

Let's dissect it. There are three parts:

我们来剖析一下。分为三个部分:

  1. cd-- This is change directory command.
  2. /d-- This switch makes cdchange both drive and directory at once. Without it you would have to do cd %~d0 & cd %~p0. (%~d0Changs active drive, cd %~p0change 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 dand pthere are modifiers of the expansion. The dforces addition of a drive letter and the padds full path.
  1. cd-- 这是更改目录命令。
  2. /d-- 此开关可以同时cd更改驱动器和目录。没有它,您将不得不这样做cd %~d0 & cd %~p0。(更改%~d0活动驱动器,cd %~p0更改目录)。
  3. %~dp0——这可以进一步分解为三个部分:
    1. %0-- 这表示批处理脚本的第零个参数。它扩展为批处理文件本身的名称。
    2. %~0-- ~there"在扩展参数周围去掉双引号 ( )。
    3. %dp0--d并且p有扩展的修饰符。该d部队另外一个驱动器号和p补充完整路径。

回答by Stephan

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

~dp0: d=drive, p=path, %0=这个批处理文件的完整路径\名称。

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

cd /d %~dp0会将路径更改为批处理文件所在的相同路径。

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

有关修饰符的更多详细信息,请参阅for /?或。 请参见有关开关。call /%~...
cd /?/d