首先,我看到这个话题,但我不明白。
题 :
在D:\path\to\file.bat有一个批处理文件, D:\path\to\file.bat包含以下内容:
echo %cd% pause输出为:
C:\它必须是D:\path\to
我究竟做错了什么?
Firstly, I saw this topic but I couldn't understand that.
Question :
There is a batch file in D:\path\to\file.bat with following content :
echo %cd% pauseOutput is :
C:\It must be D:\path\to
What am I doing wrong?
最满意答案
系统只读变量%CD%保留批处理的调用者的路径,而不是批处理文件位置。
您可以使用%0 (例如scripts\mybatch.bat )的用户输入批处理脚本本身的名称。 参数扩展可以应用于此,所以%~dp0将返回驱动器和路径到批处理脚本(例如W:\scripts\ ),并且%~f0将返回完整路径名(例如W:\scripts\mybatch.cmd )。
您可以使用以下语法来引用与批处理脚本相同的文件夹中的其他文件:
CALL %0\..\SecondBatch.cmd这甚至可以在子程序中使用, Echo %0将给出调用标签,但echo "%~nx0"会给出批处理脚本的文件名。
当%0变量被扩展时,结果将被包含在引号中。
更多关于批次参数 。
System read-only variable %CD% keeps the path of the caller of the batch, not the batch file location.
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.cmdThis 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.
When the %0 variable is expanded, the result is enclosed in quotation marks.
More on batch parameters.
更多推荐
发布评论