How to delete files end with a substring and older than N days

北城以北 提交于 2020-02-26 00:52:28

问题


I want to delete extensionless files older than 7 days whose names end with _C.

Example files:

B_C_A1_C
B_C_A2_C
B_C_A3_A
test.txt 

My code:

SET mypath=%cd%\downloads
ForFiles /p %mypath% /d -7 /c "cmd /c del /q %mypath%\*_C

When I execute the code, it deletes all files which contains _C, but I want to delete only files which end with _C and are older than 7 days.

How can I fix this?


回答1:


For more information about the forfiles documention, see Batch file to delete files older than N days.

The wildcard expression *_C selects any files which end with _C, regardless of whether it has an extension.

The final code is insanely complicated:

forfiles /p "%cd%" /m *_C /C "cmd /c \"echo @file^|find \".\" ^|^| del @path\""

Using *_C will select the following:

anything without periods_C

It will NOT select the following:

any.thing_C
anything.txt._C
anything containing periods_C


来源:https://stackoverflow.com/questions/60266385/how-to-delete-files-end-with-a-substring-and-older-than-n-days

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!