【1】需求
有10000多个文件,分布在一个大文件夹下的众多子目录,要删除文件名中包含指定字符集的文件。
如下图:
我要删除 test 目录下包含所有子文件夹的所有 文件名中带有 s1/s2/s3 的文件。
这个需要匹配的字符串已经写到 config.txt 文件中
【2】bat 代码
之前想用 %errorlevel%,但发现根本不行。不管 findstr 查没查到,永远是0 。
@echo off for /R "D:\test" %%s in (*) do ( for /f %%i in (config.txt) do ( echo %%s | findstr %%i >temp1.log for /f %%f in (temp1.log) do ( if %%f equ '' ( echo %%s skip ) else ( echo %%s need del rem del %%s ) ) ) ) pause
效果如下:
【无效案例 errorlevel】
@echo off set search_string=example set file_name=myfile.txt findstr /c:%search_string% %file_name% if %errorlevel% equ 0 ( echo Found! ) else ( echo Not found. )
标签:文件,bat,abc,文件名,%%,echo,txt From: https://www.cnblogs.com/gered/p/17384529.html