是否有Windows命令行命令,我可以用它来获取当前工作目录的完整路径?
另外,如何将此路径存储在批处理文件中使用的变量中?
cd
如果您直接使用shell,或者%cd%
如果要在批处理文件中使用它(它的行为类似于环境变量),请使用不带参数.
您可以按如下方式设置批处理/环境变量:
SET var=%cd% ECHO %var%
从Windows 7 x64 cmd.exe截取屏幕截图.
更新:如果你做了一个SET var = %cd%
而不是SET var=%cd%
,下面是会发生什么.感谢jeb.
从批处理文件中捕获当前目录
引用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.
部分.
在Unix上?
PWD
对于Windows我们可以使用
cd
对于Linux
pwd
命令就在那里.
这一直对我有用:
SET CurrentDir="%~dp0" ECHO The current file path this bat file is executing in is the following: ECHO %CurrentDir% Pause
对于Windows,cd
它本身将显示当前的工作目录.
对于UNIX和workalike系统,pwd
将执行相同的任务.您还可以$PWD
在某些shell下使用shell变量.我不确定Windows是否支持通过shell变量获取当前工作目录.
在Windows上:
CHDIR 显示当前目录的名称或更改当前目录.
在Linux中:
PWD 显示当前目录的名称.