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

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

问题描述

我想从 .bat 文件中获取文件的父目录.所以,给定一个变量设置为 "C:MyDirMyFile.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:MyDirMyFile.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:MyDirMyFile with space.txt"

或者你必须在 for 循环中的参数周围加上引号:

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天全站免登陆