如何在批处理文件中获得等效的dirname()? [英] How do I get the equivalent of dirname() in a batch file?

查看:500
本文介绍了如何在批处理文件中获得等效的dirname()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从 .bat 文件中获取文件的父目录。所以,给定变量设置为C:\MyDir\MyFile.txt,我想得到C:\ MyDir。换句话说,相当于典型UNIX环境中的 dirname()功能。这是可能吗?

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 other words, the equivalent of dirname() functionality in a typical UNIX environment. Is this possible?

推荐答案

for %%F in (%filename%) do set dirname=%%~dpF

这将设置%dirname%到存储在%filename%中的文件名的驱动器和目录。

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"

或者你必须在 / code> loop:

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 :-)

这篇关于如何在批处理文件中获得等效的dirname()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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