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

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


当前回答

作为可能的密码之一

    echo off
    for /f "usebackq tokens=* delims= " %%x in (`chdir`) do set var=%var% %%x
    echo The current directory is: "%var:~1%"

其他回答

这对我来说一直都很有效:

SET CurrentDir="%~dp0"

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

ECHO %CurrentDir%

Pause

在Windows下,输入cd作为当前工作路径。

在Linux操作系统中,pwd为当前工作路径。

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

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

如果你真的需要获取当前路径,那么现代cmd shell也有一个%CD%变量,你可以很容易地把它放到另一个变量中供参考。

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

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% -扩展到当前目录字符串。部分。

作为可能的密码之一

    echo off
    for /f "usebackq tokens=* delims= " %%x in (`chdir`) do set var=%var% %%x
    echo The current directory is: "%var:~1%"