问题描述
我想从一个.bat文件中获取文件的父目录。所以,对于一个变量设置为C:\\ MYDIR \\ MyFile.txt的,我想获得C:\\ MYDIR。在otherwords的目录名()的功能在一个典型的UNIX环境中的等价物。这可能吗?
为%% f由于(%文件名%)做设置目录名= %%〜DPF
这将设置%目录名称%
来存储在文件名称%文件名%
的驱动器和目录。
慎用含有空格,但文件名。他们要么已经与周围的报价进行设置:
设置文件名=C:\\ MYDIR \\ MyFile的与space.txt
或你必须把围绕在为
循环参数报价:
为%% f由于(%文件名%)做设置目录名= %%〜DPF
这两种方法将工作,无论是在同一时间将不会: - )
I'd like to get the parent directory of a file from within a .bat file. So, given a variable set to "C:\MyDir\MyFile.txt", I'd like to get "C:\MyDir". In otherwords, the equivalent of dirname() functionality in a typical UNIX environment. Is this possible?
for %%F in (%filename%) do set dirname=%%~dpF
This will set %dirname%
to the drive and directory of the file name stored in %filename%
.
Careful with filenames containing spaces, though. Either they have to be set with surrounding quotes:
set filename="C:\MyDir\MyFile with space.txt"
or you have to put the quotes around the argument in the for
loop:
for %%F in ("%filename%") do set dirname=%%~dpF
Either method will work, both at the same time won't :-)
这篇关于如何获取目录名()的批处理文件中相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!