bat 文件:获取父路径名
以下大部分有效。 “大部分”,因为当上下文 XML 文件试图通过相对路径名包含另一个文件时,使用 SOMETHING..\tasks\ 路径名会使 Spring 感到困惑。所以,我似乎需要一种在 BAT 文件中将变量设置为路径名的父目录的方法。
set ROOT=%~dp0
java -Xmx1g -jar %ROOT%\..\lib\ajar.jar %ROOT%\..\tasks\fas-model.xml tasks
The following mostly works. 'Mostly', because the use of the SOMETHING..\tasks\ pathname confuses Spring when a context XML file tries to include another by relative pathname. So, what I seem to need is a way, in a BAT file, of setting a variable to the parent directory of a pathname.
set ROOT=%~dp0
java -Xmx1g -jar %ROOT%\..\lib\ajar.jar %ROOT%\..\tasks\fas-model.xml tasks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要解析相对路径名,您可以使用子例程调用。
在批处理文件的末尾放置以下行:
这是一个子例程,它将其第一个参数解析为完整路径 (
%~f1
) 并将结果存储到名为的(全局)变量第二个参数您可以像这样使用例程:
调用后,您可以使用变量
%PARENT_ROOT%
,该变量包含%ROOT%
变量中包含的父路径名。完整的批处理文件应如下所示:
To resolve a relative path name you can utilize a sub routine call.
At the end of your batch file place the following lines:
This is a sub routine that resolves its first parameter to a full path (
%~f1
) and stores the result to the (global) variable named by the 2nd parameterYou can use the routine like this:
After the call you can use the variable
%PARENT_ROOT%
that contains the parent path name contained in the%ROOT%
variable.Your complete batch file should look like this:
这是一个内衬
Here is a one liner
要扩展接受的答案,如果您想继续沿着路径前进(例如获取父目录的父目录) ),去掉尾部斜杠:
用法:
将从
C:\My\Path\Child\file.bat
生成C:\My\Path
。如果我更好地理解它,我建议使用“包装函数”,这样您就可以
CALL :REMOVE_SEGMENTS %path% 3 PARENT
从%path%
中删除最后 3 个段。To expand on the accepted answer, if you want to keep marching up the path (to get the parent's parent directory, for example), strip the trailing slash:
Usage:
would yield
C:\My\Path
fromC:\My\Path\Child\file.bat
.If I understood it better, I'd suggest a "wrapper function" so you could
CALL :REMOVE_SEGMENTS %path% 3 PARENT
to strip the last 3 segments from%path%
.