2009/10/15

Batch: delete files older than X days

CURRENT CONFIGURATION: Windows XP, Windows 2003, Windows 2008

OBJECTIVE: Delete by batch scrip files older than X days. Call this script from different scripts or from scheduled task.

SOLUTION:
I wrote batch file for that. The script requires forfiles.exe. Forfiles.exe exist by default on Windows 2003, 2008 servers.  Can be downloaded with Microsoft Windows NT Server 4.0 Resource Kit.

--- Start of delete_old_files.cmd batch file ---

@echo on
:: *****************************************************************************
:: Author:  Vadim Zenin http://vadimszenins.blogspot.com
::  Version: 1.02
::  Date:     15/09/2009 10:21:31
::
::  delete files in folder with extentions
::  with/without recurse into subdirectories
::  with/without logging
::
:: This code is made available as is, without warranty of any kind. The entire
:: risk of the use or the results from the use of this code remains with the user.
:: *****************************************************************************
@echo off
::echo Variables 1: %1; 2: %2; 3: %3; 4: %4; 5: %5

@SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION

:: Grab a file name and extension only
SET SCRIPTNAME=%~nx0
:: Grab a file name only (prefix)
SET SCRIPTONLYNAME=%~n0
:: Replace "
SET SCRIPTNAME=%SCRIPTNAME:"=%
SET SCRIPTONLYNAME=%SCRIPTONLYNAME:"=%
:: Replace spases
SET SCRIPTONLYNAME=%SCRIPTONLYNAME: =%
SET TOOLS=C:\tools
SET LOGDIR=%TOOLS%\logs

SET SOURCEDIR=%1
SET SOURCEFILEEXT=%2
SET DAYSOLD=%3
SET RECURSE=%4
SET LOGGING=%5
:: Replace "
SET SOURCEDIR=%SOURCEDIR:"=%
:: "
SET FILENAME1=%SOURCEDIR%
:: Replace \
SET FILENAME1=%FILENAME1:\=%
:: Replace :
SET FILENAME1=%FILENAME1::=%
:: Replace -
SET FILENAME1=%FILENAME1:-=%
:: Replace _
SET FILENAME1=%FILENAME1:_=%
:: cut to 32 symbols
SET FILENAME1=%FILENAME1:~0,32%
SET LOGFILE=%LOGDIR%\%SCRIPTONLYNAME%_%FILENAME1%.log
SET TEMPLISTFILE1=%LOGDIR%\%SCRIPTONLYNAME%_%FILENAME1%.txt
:: Require
:: forfiles.exe
:: =============================================================================
SET RETURN=0

if not exist %LOGDIR% md %LOGDIR%
echo. >>%LOGFILE%
echo ============================================================================= >>%LOGFILE%
echo %DATE% %TIME% %SCRIPTNAME% has started >>%LOGFILE%
echo ============================================================================= >>%LOGFILE%
echo Variables SOURCEDIR: %1; SOURCEFILEEXTENTION: %2; DAYSOLD: %3; RECURSE: %4; LOGGING: %5 >>%LOGFILE%
@if "%1"=="" goto usage
@if "%1"=="/?" goto usage
@if "%1"=="-?" goto usage
@if "%4"=="" goto usage

@echo Delete files older then %DAYSOLD% days with %RECURSE% in directory >>%LOGFILE%
@echo %SOURCEDIR%\%SOURCEFILEEXT% >>%LOGFILE%
if "%RECURSE%"=="NORECURSE" SET RECOPT=
if "%RECURSE%"=="RECURSE" SET RECOPT=/s
@echo RECOPT (recursive option): %RECOPT% >>%LOGFILE%

FORFILES /p %SOURCEDIR% %RECOPT% /d -%DAYSOLD% /m %SOURCEFILEEXT% /c "CMD /C Echo @PATH" > %TEMPLISTFILE1%
type %TEMPLISTFILE1% >>%LOGFILE%
echo. >>%LOGFILE%
@Sleep 1

FOR /f %%u IN (%TEMPLISTFILE1%) DO (
    if exist %%u @ERASE /F /Q %%u >>%LOGFILE% )
@Sleep 1
@goto END

:USAGE
@echo Usage: >>%LOGFILE%
@echo  %SCRIPTNAME% ^ ^ ^ ^ ^(optinal) >>%LOGFILE%
@echo Examples: delete all *.zip in folder x:\archive older than 7 days
@echo           %SCRIPTNAME% x:\archive *.zip 7 RECURSE LOGGING (into subfolders with log file) >>%LOGFILE%
@echo           %SCRIPTNAME% x:\archive *.zip 7 NORECURSE (in current folder only with log file) >>%LOGFILE%
@echo           %SCRIPTNAME% x:\archive *.zip 7 RECURSE NOLOG (into subfolders without log file) >>%LOGFILE%
@echo. >>%LOGFILE%
::@notepad %LOGFILE%
exit /b 128

:END
echo %DATE% %TIME% %SCRIPTNAME% has finished with code %RETURN% >>%LOGFILE%
::notepad %LOGFILE%
echo. >>%LOGFILE%
if "%LOGGING%"=="NOLOG" ERASE /F /Q %LOGFILE%
exit %RETURN%



--- The end of delete_old_files.cmd batch file ---



DOWNLOAD:
Download delete_old_files.zip md5: e695867259b0956488bd37448f2be44e

Комментариев нет: