Batch file to delete files older than N days

前端 未结 24 3003
没有蜡笔的小新
没有蜡笔的小新 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条回答
  •  萌比男神i
    2020-11-21 11:50

    This one did it for me. It works with a date and you can substract the wanted amount in years to go back in time:

    @echo off
    
    set m=%date:~-7,2%
    set /A m
    set dateYear=%date:~-4,4%
    set /A dateYear -= 2
    set DATE_DIR=%date:~-10,2%.%m%.%dateYear% 
    
    forfiles /p "C:\your\path\here\" /s /m *.* /d -%DATE_DIR% /c "cmd /c del @path /F"
    
    pause
    

    the /F in the cmd /c del @path /F forces the specific file to be deleted in some the cases the file can be read-only.

    the dateYear is the year Variable and there you can change the substract to your own needs

提交回复
热议问题