batch-file - 批处理文件删除 X 天旧文件排除几个文件
问题描述
我想从目录中删除 X 天前的文件,但想排除特定文件(*例外)。我正在使用以下命令,但似乎对我不起作用。
for /f "delims=" %%a in ('forfiles /p "%userprofile%\.." /d -180 /c "cmd /c if @isdir==TRUE echo @file"^|findstr /vig:"%userprofile%\exception.txt"') do echo rd /s /q "%%~a"
解决方案
Instead of using ForFiles
you could use RoboCopy
:
@For /F "Tokens=*" %%A In ('RoboCopy "%UserProfile%\.." Null /L /MinAge:180 /NC /NDL /NJH /NJS /NP /NS /XF exception*') Do @Del "%%A"
Enter RoboCopy /?
at the Command Prompt for its usage information.
After re-reading your question, together with your code, it isn't clear whether you're wanting to exclude files named, exception*
, which is what my answer above does, or to exclude files as listed inside a file named exception.txt
. If it's the latter then you'd need to build a string of your exclusions first to append to the /XF
list.
If your file only contains a few relatively short exclusion masks then you may be able to do it like this:
@Echo Off
Set "EX="
For /F "UseBackQ Delims=" %%A In ("%UserProfile%\exception.txt") Do (
If Not Defined EX (Set "EX=%%A ") Else Call Set "EX=%%EX%%%%A ")
For /F "Tokens=*" %%A In ('RoboCopy "%UserProfile%\.." Null /L /MinAge:180 /NC /NDL /NJH /NJS /NP /NS /XF %EX%') Do Del "%%A"
推荐阅读
- javascript - Angular 6 单元测试:如何使用 test.ts 运行业力测试
- php - acf_add_option() 未在管理仪表板中显示任何选项
- sql - 将逗号分隔的列数据拆分为具有 50 多个字段的附加列
- jquery - 如何在按下的按钮上动态设置类?
- java - 如何调用 CallBack(); 执行成功后再次运行?
- html - xslfo, xsl, pdf, html: 在某些页面之后为动态生成的 pdf 文档重置页脚页码
- java - MP4 视频的文件输入/输出流
- django - 在 django-rest-framework 中发送电子邮件的最佳位置在哪里?
- php - 尝试使用 isset 设置值但仍然出现错误
- python - 合并并创建一个包含相同 id 的所有记录的新 JSON 数组