如何在单个命令中不带反斜杠的情况下获取批处理脚本的路径? [英] How to get the path of a batch script without the trailing backslash in a single command?
问题描述
假设我希望从批处理脚本本身中获取批处理脚本的绝对路径,但不要在其后加反斜杠.通常,我是这样进行的:
Suppose I wish to get the absolute path of a batch script from within the batch script itself, but without a trailing backslash. Normally, I do it this way:
SET BuildDir=%~dp0
SET BuildDir=%BuildDir:~0,-1%
第一个语句获取带有尾部反斜杠的路径,第二行删除最后一个字符,即反斜杠.有没有办法将这两个语句合并为一行代码?
The first statement gets the path with the trailing backslash and the second line removes the last character, i.e. the backslash. Is there a way to combine these two statements into a single line of code?
推荐答案
在许多软件上,添加尾随点在语义上是等效的,而不是删除尾随反斜杠.
Instead of removing the trailing backslash, adding a trailing dot is semantically equivalent for many software.
C:\ Windows 等同于 C:\ Windows \.
C:\Windows is equivalent to C:\Windows\.
echo %dp0
>C:\Windows\
echo %dp0.
>C:\Windows\.
例如,robocopy仅接受没有尾随空格的目录.
此错误提示:
For example, robocopy accepts only directories without trailing spaces.
This errors out:
robocopy "C:\myDir" %~dp0
这是成功的:
robocopy "C:\myDir" %~dp0.
这篇关于如何在单个命令中不带反斜杠的情况下获取批处理脚本的路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!