如何测试文件是否是批处理脚本中的目录? [英] How to test if a file is a directory in a batch script?

查看:18
本文介绍了如何测试文件是否是批处理脚本中的目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么办法可以判断一个文件是否是一个目录?

Is there any way to find out if a file is a directory?

我在一个变量中有文件名.在 Perl 中,我可以这样做:

I have the file name in a variable. In Perl I can do this:

if(-d $var) { print "it's a directory
" }

推荐答案

你可以这样做:

IF EXIST %VAR%NUL ECHO It's a directory

然而,这仅适用于名称中没有空格的目录.当您在变量周围添加引号以处理空格时,它将停止工作.要处理带有空格的目录,请将文件名转换为 8.3 短格式,如下所示:

However, this only works for directories without spaces in their names. When you add quotes round the variable to handle the spaces it will stop working. To handle directories with spaces, convert the filename to short 8.3 format as follows:

FOR %%i IN (%VAR%) DO IF EXIST %%~siNUL ECHO It's a directory

%%~si%%i 转换为 8.3 文件名.要查看您可以使用 FOR 变量执行的所有其他技巧,请在命令提示符下输入 HELP FOR.

The %%~si converts %%i to an 8.3 filename. To see all the other tricks you can perform with FOR variables enter HELP FOR at a command prompt.

(注意 - 上面给出的示例是在批处理文件中工作的格式.要使其在命令行上工作,请将 %% 替换为 %在两个地方.)

(Note - the example given above is in the format to work in a batch file. To get it work on the command line, replace the %% with % in both places.)

这篇关于如何测试文件是否是批处理脚本中的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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