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
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