获取当前目录中的批处理文件 [英] Get current batchfile directory
问题描述
首先,我看到这个话题,但我不明白这一点。
Firstly, I saw this topic but I couldn't understand that.
问:
有在 D的批处理文件:\\路径\\为\\ file.bat
与以下内容:
echo %cd%
pause
输出是:
C:\
必须是 D:\\路径\\为
我在做什么错了?
推荐答案
系统只读变量%CD%
保持批次,调用者的路径不批处理文件的位置。
System read-only variable %CD%
keeps the path of the caller of the batch, not the batch file location.
您可以得到批处理脚本本身的名称与 0%
(如键入脚本由用户\\ mybatch.bat
)。 参数的扩展可以应用到这让%〜DP0
将返回驱动器和路径到批处理脚本(如 W:\\脚本\\
)和%〜F0 code>将返回全路径名(例如:
W:\\脚本\\ mybatch.cmd
)。
You can get the name of the batch script itself as typed by the user with %0
(e.g. scripts\mybatch.bat
). Parameter extensions can be applied to this so %~dp0
will return the Drive and Path to the batch script (e.g. W:\scripts\
) and %~f0
will return the full pathname (e.g. W:\scripts\mybatch.cmd
).
您可以使用此语法参考其他文件在同一文件夹作为批处理脚本:
You can refer to other files in the same folder as the batch script by using this syntax:
CALL %0\..\SecondBatch.cmd
这甚至可以在子程序中使用,回声%0
会给呼叫标签,但回声%〜NX0
会给你的批处理脚本的文件名。
This can even be used in a subroutine, Echo %0
will give the call label but, echo "%~nx0"
will give you the filename of the batch script.
在该 0%
变量扩大,其结果是用引号括起来。
When the %0
variable is expanded, the result is enclosed in quotation marks.
这篇关于获取当前目录中的批处理文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!