在目录中搜索多个文件并提供反馈 [英] Searching for multiple files in a directory and provide feedback

查看:104
本文介绍了在目录中搜索多个文件并提供反馈的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个的.bat 文件,该文件会发现在一个目录多个文件,如果它们存在或不提供反馈。

I'm trying to create a .bat file that will find multiple files in a single directory, and provide feedback if they exist or not.

该怎么做?

推荐答案

在它最简单的形式:结果
DIR型式1,模式2,Pattern3> NUL 2 - ;&放大器; 1安培;&安培;回声发现结果
DIR 将搜索指定的文件或目录匹配模式(可以使用wildchars)。如果你只想要的文件,使用DIR / A-D 结果
> NUL 2 - ;&安培; 苏$从中结果输出1个P $ psses
&放大器;&安培; 执行意味着只有previous命令成功(即DIR发现与该模式的文件/目录)。您也可以使用 || 意思相反(如previous失败)。

In it's simplest form:
dir Pattern1,Pattern2,Pattern3 >nul 2>&1 && echo found
dir will search for files or directories matching patterns specified (you may use wildchars). If you want only files, use dir /A-D
>nul 2>&1 supresses output from it
&& means execute only if previous command was successful (ie dir found a file/dir with that pattern). You may also use || meaning opposite (if previous failed).

如果您需要检查是否存在的所有模式的简单链,而不是:结果
(DIR样式1&放大器;&安培; DIR模式2和放大器;&安培; DIR Pattern3)GT; NUL 2 - ;&放大器; 1安培;&安培;回声发现

If you need to check if all patterns exist simply chain instead:
(dir Pattern1 && dir Pattern2 && dir Pattern3) >nul 2>&1 && echo found

如果你想让你的批次更具可读性(!)使用如果errolevel / 如果没有ERRORLEVEL 来代替。
ERRORLEVEL由DIR 1设置,如果没有找到任何东西,为0;如果找到一个匹配。

If you want your batch more readable (!) use if errolevel / if not errorlevel instead. Errorlevel is set by dir to 1 if it does not find anything, to 0 if it finds a match.

当然,你可以使用任何其他的命令(S),而不是回声

Of course you may use any other command(s) instead of echo

这篇关于在目录中搜索多个文件并提供反馈的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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