如何在 Windows 中获取批处理脚本的路径?

问题描述:

我知道 %0 包含批处理脚本的完整路径,例如c:path omyfileabc.bat

I know that %0 contains the full path of the batch script, e.g. c:path omyfileabc.bat

我希望 path 等于 c:path omyfile

我怎么能做到这一点?

%~dp0 将是目录.这里有一些关于所有路径修饰符的文档.有趣的东西:-)

%~dp0 will be the directory. Here's some documentation on all of the path modifiers. Fun stuff :-)

要删除最后的反斜杠,您可以使用 :n,m 子字符串语法,如下所示:

To remove the final backslash, you can use the :n,m substring syntax, like so:

SET mypath=%~dp0
echo %mypath:~0,-1%

不幸的是,我认为没有办法将 %0 语法与 :~n,m 语法结合起来.

I don't believe there's a way to combine the %0 syntax with the :~n,m syntax, unfortunately.