windows 如何获取windows批处理的父文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16623780/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get windows batch's parent folder
提问by Tobia
I'm writing a batch file, I need to get the parent folder of this bat file. Is it possibile? NB I mean the parent folder of the batch file not of the current directory of the prompt calling that batch.
我正在写一个批处理文件,我需要获取这个bat文件的父文件夹。有可能吗?注意我的意思是批处理文件的父文件夹,而不是调用该批处理的提示的当前目录。
Thanks
谢谢
回答by Endoro
The parent folder of a Batch is in the Variable %~dp0
located. Example:
Batch 的父文件夹%~dp0
位于Variable 中。例子:
@echo off&setlocal
for %%i in ("%~dp0.") do set "folder=%%~fi"
echo %folder%
回答by cpcolella
Endoro's answer doesn't work for me either but this does. This is what I use myself to do this.
Endoro 的答案对我也不起作用,但确实如此。这就是我用自己来做的。
V3
V3
One of these should do the right thing
其中之一应该做正确的事情
for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~nxJ
echo %ParentFolderName%
for %%I in ("%~dp0.") do for %%J in ("%%~dpI.") do set ParentFolderName=%%~dpnxJ
echo %ParentFolderName%
V2:
V2:
for %%I in ("%~dp0\.") do set ParentFolderName=%%~nxI
echo %ParentFolderName%
V1:
V1:
This one gets the parent directory of the current working directory
这个获取当前工作目录的父目录
for %%I in (..) do set ParentFolderName=%%~nI%%~xI
echo %ParentFolderName%
Reference: For | Microsoft Docs
参考:对于 | 微软文档