且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows批处理脚本以搜索要删除的特定文件

更新时间:2023-12-05 20:18:34

您真的不解释您的象形文字是什么意思.

You really don't explain what your hieroglypics mean.

在批处理文件中,

@echo off
setlocal
dir /s "c:\local root\Excel_*.xls"

将显示所有以Excel_开头且扩展名为.xls

would show all of the files matching starting Excel_ with extension .xls

(如果您要这样做,只需将dir替换为del即可删除它们;添加>nul可以禁止显示消息;添加2>nul可以禁止显示错误消息)

(and if that's what you want, simply replacing dir with del would delete them; adding >nul would suppress messages; adding 2>nul suppresses error messages)

如果您希望文件以Excel_开头,然后是任何纯字母开头,则

If you want files starting Excel_ then followed by any alphas-only, then

for /f "delims=" %%a in ('dir /b /s /a-d Excel_*.xls ^| findstr /E /R "\\Excel_[a-z]*\.xls" ') do echo "%%a"

dir产生/b(基本)格式(即仅文件名)的目录列表/s-带有子目录(附加完整目录名称),而/a-d禁止显示目录名称.它通过管道传送到findstr以过滤出所需的文件名.结果逐行分配给%%a,并且delims=表示不要标记数据"

The dir produces a directory list in /b (basic) form (ie. filename-only) /s - with subdirectories (which attaches the full directory name) and the /a-d suppresses directorynames. This is piped to findstr to filter out the required filenames. The result is assigned to %%a line-by-line, and the delims= means "don't tokenise the data"

应向您显示所有符合该条件的文件-但大小写敏感;将/i添加到findstr开关以使其不区分大小写. (/r表示findstr受限制的实现中的"regex";/e表示结束"-我倾向于在$上使用它们).\\旨在实现转义的\以确保文件名匹配完成后(即找不到"some_prefixExcel_whatever.xls")-我将把\.打算做的事情留给您发挥想象力...

should show you all the files matching that criterion - but it would be case-sensitive; add /i to the findstr switches to make it case-insensitive. (/r means "regex" within findstr's restricted implementation; /e means "ends" - I tend to use these over $) The \\ in intended to implement escaped \ to ensure the filename match is complete (ie do't find "some_prefixExcel_whatever.xls) - I'll leave what \. is intended to do to your imagination...

(同样,将echo更改为del以删除并在需要时添加重定向程序)

(again, change echo to del to delete and add in the redirection palaver if required)

另一个正则表达式-好吧,跟随弹跳球.似乎您想要的文件名称以presentation开头的.ppt文件,然后是一系列可能的数字,然后是一系列的字母.我建议

And the other regex - well, follow the bouncing ball. It would appear you want .ppt files with names starting presentation followed by a possible series of numerics then by a series of alphabetics. I'd suggest

findstr /e /r "\\presentation[0-9]*[a-z]*\.ppt" for that task.