Batch file to delete files older than N days

前端 未结 24 2464
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 11:11

I am looking for a way to delete all files older than 7 days in a batch file. I\'ve searched around the web, and found some examples with hundreds of lines of code, and oth

24条回答
  •  野的像风
    2020-11-21 11:51

    Have a look at my answer to a similar question:

    REM del_old.bat
    REM usage: del_old MM-DD-YYY
    for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
    for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"
    

    This deletes files older than a given date. I'm sure it can be modified to go back seven days from the current date.

    update: I notice that HerbCSO has improved on the above script. I recommend using his version instead.

提交回复
热议问题