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

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

问题描述

有没有办法找出文件是否是目录?

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\n" }

推荐答案

您可以这样做:

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 %%~si\NUL 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天全站免登陆