Windows shell命令获取当前目录的完整路径?

use*_*958 201 windows command-line cmd batch-file

是否有Windows命令行命令,我可以用它来获取当前工作目录的完整路径?

另外,如何将此路径存储在批处理文件中使用的变量中?

Tre*_*ble 313

cd如果您直接使用shell,或者%cd%如果要在批处理文件中使用它(它的行为类似于环境变量),请使用不带参数.

  • *你是如何理解*他想从*那里说出来的?并且,在dos和windows cmd下,它通常只是"cd" (10认同)
  • 老实说,我想不出他们可能会问的任何其他问题. (10认同)

gma*_*n23 93

您可以按如下方式设置批处理/环境变量:

SET var=%cd%
ECHO %var%
Run Code Online (Sandbox Code Playgroud)

从Windows 7 x64 cmd.exe截取屏幕截图.

在此输入图像描述

更新:如果你做了一个SET var = %cd%而不是SET var=%cd%,下面是会发生什么.感谢jeb.

在此输入图像描述

从批处理文件中捕获当前目录

  • 但它不起作用,因为`SET var =%cd%`将变量`var <space>`中的值放入`var`中.您应该在SET命令中避免使用空格 (6认同)
  • 根据问题,此答案实际上应该是公认的答案。 (2认同)
  • 根据问题,这个答案实际上应该是公认的。 (2认同)

Pat*_*uff 47

引用set命令(set /?)的Windows帮助:

If Command Extensions are enabled, then there are several dynamic
environment variables that can be expanded but which don't show up in
the list of variables displayed by SET.  These variable values are
computed dynamically each time the value of the variable is expanded.
If the user explicitly defines a variable with one of these names, then
that definition will override the dynamic one described below:

%CD% - expands to the current directory string.

%DATE% - expands to current date using same format as DATE command.

%TIME% - expands to current time using same format as TIME command.

%RANDOM% - expands to a random decimal number between 0 and 32767.

%ERRORLEVEL% - expands to the current ERRORLEVEL value

%CMDEXTVERSION% - expands to the current Command Processor Extensions
    version number.

%CMDCMDLINE% - expands to the original command line that invoked the
    Command Processor.

注意%CD% - expands to the current directory string.部分.


Ste*_*ial 25

在Unix上?

PWD

  • OP最初要求"命令"并没有指定操作系统.现在已经指定了操作系统,因此这个答案已不再适用. (2认同)

Nik*_* K. 15

对于Windows我们可以使用

cd

对于Linux

pwd

命令就在那里.


小智 13

这一直对我有用:

SET CurrentDir="%~dp0"

ECHO The current file path this bat file is executing in is the following:

ECHO %CurrentDir%

Pause
Run Code Online (Sandbox Code Playgroud)

  • 由于回答了 OP 的问题而被点赞……并为我提供了我正在寻找的确切解决方案。谢谢! (3认同)
  • 这样做是错误的 - 找到批处理脚本的路径,而不是当前目录. (2认同)
  • %cd% 是错误的答案。如果使用管理员访问权限启动 CMD 窗口,该目录将显示为 %windir%\system32\。%~dp0% 显示相应的目录,即使在管理员启动的 CMD 窗口中也是如此。 (2认同)

Mic*_*sch 7

对于Windows,cd它本身将显示当前的工作目录.

对于UNIX和workalike系统,pwd将执行相同的任务.您还可以$PWD在某些shell下使用shell变量.我不确定Windows是否支持通过shell变量获取当前工作目录.

  • 他需要在批处理文件中. (2认同)

Aja*_*apu 6

在Windows上:

CHDIR 显示当前目录的名称或更改当前目录.

在Linux中:

PWD 显示当前目录的名称.


Vis*_*ant 6

.bat在 下创建一个文件System32,让我们将其命名copypath.bat为复制当前路径的命令可以是

echo %cd% | clip
Run Code Online (Sandbox Code Playgroud)

说明:

%cd%会给你当前的路径

CLIP

Description:
    Redirects output of command line tools to the Windows clipboard.
    This text output can then be pasted into other programs.

Parameter List:
    /?                  Displays this help message.

Examples:
    DIR | CLIP          Places a copy of the current directory
                        listing into the Windows clipboard.

    CLIP < README.TXT   Places a copy of the text from readme.txt
                        on to the Windows clipboard.
Run Code Online (Sandbox Code Playgroud)

现在copypath可以从任何地方获得。


Mar*_*ark 5

根据 chdir 帖子的评论中的后续问题(将数据存储在变量中),我敢打赌他想存储当前路径以在更改目录后恢复它。

原始用户应该查看“pushd”,它更改目录并将当前目录推送到可以用“popd”恢复的堆栈上。在任何现代 Windows cmd shell 上,这是制作批处理文件时要走的路。

如果您确实需要获取当前路径,那么现代 cmd shell 也有一个 %CD% 变量,您可以轻松地将其塞入另一个变量中以供参考。