2

按关键字检测文件(夹)是否存在的批处理程序

 2 years ago
source link: https://www.lfhacks.com/tech/batch-file-finder/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

按关键字检测文件(夹)是否存在的批处理程序

发布日期 2011-03-23
最后修改 2022-02-03
阅读 2 分钟
阅读量 1184
669.jpg
扫一扫,转发文章 batch-file-finder.png
本文发布至今已有10年零323天,可能不再适用,请谨慎对待。

在使用了 批量搜索并复制/剪切文件的批处理程序 中的批处理程序之后,往往还需要和原有的关键字比对,知道搜索到了多少文件。这就需要一个工具,能够批量检测一批关键字是否存在于一堆文件里。

比如有如下一个文件列表:

AA1BB2DD3BBC1EE2Abb

希望从中批量搜索文件名包含

AABBCC

的文件,也就是:

AA1BB2BBC1Abb

有点类似Excel里的vlookup命令,所不同的是用批处理程序可以直接在文件目录下操作,而不需要导出文件名的列表。

批量搜索并复制/剪切文件的批处理程序 批量搜索 类似,实现这个任务,仍然需要强大的for命令,并且结合find命令,就能完成这一任务。同时也能检测文件夹是否存在。最后将找到和未找到的关键词分别输出到两个文本文件下。

首先把关键字保存在名叫 list.txt 的文本文件里,每个关键字占一行。注意不能有空格、空行、空回车。例如:

#list.txtAABBCC

下面是批处理文件的代码,把下面的代码保存成以 *.bat为扩展名的文件:

::Batch search & check::by lfhacks.com, Apr 1, 2009 @echo offsetlocal enabledelayedexpansioncolor 1Eecho 本程序检查当前目录下是否存在给定关键字的文件(夹)echo.echo by www.lfhacks.comecho.echo 例如,检查缺少的文件名if not exist list.txt (echo.echo 请先将关键字列表保存为list.txt...pause \> NULexit)echo.echo 要检查文件还是目录?echo.set /p opt=文件(f) or 目录(d):if opt NEQ f if opt NEQ d (echo 回应错误。pause \>NULexit)if optd (dir /AD /B \* >tempp)if %opt%f (set /p ext=指定文件扩展名(不包括".",不指定请输入\*):dir /A-D /B \*.![](ext) \>tempp) if exist result.txt del result.txtset count=0for /f "tokens=\*" %%i in (list.txt) do (find "%%i" tempp \>NULecho 检查 %%i...if ![](errorlevel) EQU 1 (echo ---- Not Found ----echo %%i \>\> Not_found.txtset /a count+=1)ELSE (echo %%i \>\> Found.txt))del temppecho.if count NEQ 0 echo 任务完成!共有 count个关键字不存在,结果已保存到Not_found.txtif count EQU 0 echo 列表中的关键字都存在echo 存在的关键字保存到Found.txt pause \>NULexit
cc-by-nc.gif

若无特别说明,本站文章均为原创,并采用 署名协议 CC-BY-NC 授权。
欢迎转载,惟请保留原文链接,且不得用于商业用途。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK