如何获取目录名()的批处理文件中相同呢? [英] How do I get the equivalent of dirname() in a batch file?

查看:231
本文介绍了如何获取目录名()的批处理文件中相同呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个.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 :-)

这篇关于如何获取目录名()的批处理文件中相同呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆