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

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

问题描述

有没有办法说,如果一个文件是目录?我有一个变量名。在Perl我可以做到这一点:

 如果(-d $ VAR){打印这是一个目录\\ n}


解决方案

您可以像这样:

 (如果存在)的%VAR%\\ NUL ECHO这是一个目录

不过,这仅适用于目录,而在其名称中的空格。当一轮变量添加引号来操作的空间就会停止工作。与空间处理目录,文件名转换为短8.3格式如下:

  FOR %% I IN(VAR%%)DO是否存在%%〜SI \\ NUL ECHO这是一个目录

%%〜SI 转换 %%我来一个8.3文件名。要查看所有可以用输入变量 HELP FOR 执行其他技巧在命令提示符下。

(注 - 上面给出的例子是在格式的批处理文件的工作要得到它的命令行上工作,则更换 %% 在这两个地方。)

Is there any way to say if a file is a directory? I have the filename in a variable. In Perl I can do this:

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

解决方案

You can do it like so:

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

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

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