As always: there are a hundred different ways to do the same thing. Here is a batch file I use to cleanup old temp and log files from servers. It does recurse subdirectories, but as I didn't have a need to delete empty folders I didn't implement, if you need that functionality the vbscript may be a better fit.
Make a batch file called purge_old_files.bat and paste in the following code. Just modify the 'list of directories to clean' section with your desired directories, one per line following the existing pattern.
Code:
@echo off
SET tempX=dirs.tmp.txt
IF EXIST %tempX% del %tempX%
::::::::::::::::::::::::::::::::::
::the list of directories to clean
::::::::::::::::::::::::::::::::::
echo "c:\windows\system32\logfiles" >> %tempX%
echo "D:\Program Files\Exchsrvr\server.log" >> %tempX%
echo "D:\Program Files\Trend Micro\Security Server\PCCSRV\Log" >> %tempX%
:: the work
IF "%~1"=="" (GOTO ERROR) else SET DAYS=%1
echo.
echo Deleting the following files %DAYS% days old or older.
echo.
FOR /F "delims=," %%i IN (%tempX%) DO forfiles -p %%i -s -m *.* -d -%DAYS% -c "Cmd /C echo @Path & del @Path"
GOTO CLEANUP
:ERROR
echo.
echo You used the utility incorrectly.
echo Usage: %0 DAYS
echo All files older than DAYS days old will be deleted from the following directories (and subdirectories.)
echo.
FOR /F "delims=," %%i IN (%tempX%) DO echo %%i
:CLEANUP
IF EXIST %tempX% del %tempX%
Bookmarks