获取当前的batchfile目录(Get current batchfile directory)

编程入门 行业动态 更新时间:2024-10-26 22:25:07
获取当前的batchfile目录(Get current batchfile directory)

首先,我看到这个话题,但我不明白。

题 :

在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% pause

Output 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.cmd

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.

When the %0 variable is expanded, the result is enclosed in quotation marks.

More on batch parameters.

更多推荐

本文发布于:2023-08-01 18:10:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1362241.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:目录   batchfile   directory   current

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!