@echo off setlocal EnableExtensions if "%~2"=="" ( echo Usage: %~nx0 folder days [delete] echo Without delete the script only lists files. exit /b 2 ) set "DIR=%~1" set "DAYS=%~2" set "MODE=%~3" if not exist "%DIR%\" ( echo Folder not found: %DIR% exit /b 1 ) set "VALID=1" for /f "delims=0123456789" %%A in ("%DAYS%") do set "VALID=0" if "%VALID%"=="0" ( echo Days must be a positive number. exit /b 2 ) if "%DAYS%"=="0" ( echo Days must be a positive number. exit /b 2 ) if not "%DAYS%"=="0" if "%DAYS:~0,1%"=="0" ( echo Days must be a positive number. exit /b 2 ) if /i "%MODE%"=="delete" ( echo Deleting zip files older than %DAYS% days in "%DIR%" forfiles /P "%DIR%" /M *.zip /D -%DAYS% /C "cmd /c del /q @path" ) else ( echo Zip files older than %DAYS% days: forfiles /P "%DIR%" /M *.zip /D -%DAYS% /C "cmd /c echo @path" ) set "RC=%ERRORLEVEL%" if %RC% EQU 1 ( echo No matching files. exit /b 0 ) if not %RC% EQU 0 ( echo forfiles failed with code %RC%. exit /b 1 ) echo Done. exit /b 0