This script excepts command line arguments and will either print to the command screen or delete files older than or less than the specified number of days.
Script
Code:
@echo off
SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
:: ######################################################################################################
:: ScriptName...............: FileAges.bat
:: Original Author..........: http://www.dostips.com
:: Modified By..............: Cleptography
:: ScriptPurpose............: Prints or Deletes files older than or newer than a specified number of days
:: Required Files...........: N/A
:: Date.....................: 03/16/2011
:: Time.....................: 16:01:35.16
:: System Requirements......: Windows XP/Vista/7
:: Syntax...................: FileAges (Switch) (Val) (Switch) ("DirPath")
:: Examples.................: FileAges "%HomeDrive%\"
:: .........................: FileAges MoreThan 250 Print "%HomeDrive%\"
:: .........................: FileAges LessThan 250 Print "%HomeDrive%\"
:: .........................: FileAges MoreThan 250 Del "%HomeDrive%\"
:: .........................: FileAges LessThan 250 Del "%HomeDrive%\"
:: Notes....................: Original script printed all files only and in the current directory from
:: .........................: which it was ran.
:: .........................: This version excepts command arguments to specify exact days and specific
:: .........................: directories, with the option to print or delete those files.
:: .........................: Future plans to specify exact file extensions
::######################################################################################################
set str=%*
for /f "tokens=1,2,3,4" %%a in ("%str%") do (
if /i "%%a"=="LessThan" (
if /i "%%c"=="print" (
set AgeName=%%a
set AgeNumb=%%b
set W2Do=%%c
set Directory=%%d
goto :NewPrint
) else if /i "%%a"=="LessThan" (
if /i "%%c"=="del" (
set AgeName=%%a
set AgeNumb=%%b
set W2Do=%%c
set Directory=%%d
goto :NewDelete
)
)
)
)
for /f "tokens=2,3,4,5" %%a in ("%str%") do (
if /i "%%a"=="MoreThan" (
if /i "%%c"=="print" (
set AgeName=%%a
set AgeNumb=%%b
set W2Do=%%c
set Directory=%%d
goto :OldPrint
) else if /i "%%a"=="MoreThan" (
if /i "%%c"=="del" (
set AgeName=%%a
set AgeNumb=%%b
set W2Do=%%c
set Directory=%%d
goto :OldDelete
)
)
)
)
:ShowAll
set directory=%~1
if not defined directory echo.Error No Arguments Specified&& goto :eof
cd "%directory%"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
echo.%%~nxF is !diff! days old
)
goto :eof
:OldPrint
cd "%directory%"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
if !diff! GEQ %AgeNumb% (echo.%%~nxF is !diff! days old
)
)
goto :eof
:OldDelete
cd "%directory%"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
if !diff! GEQ %AgeNumb% (del /f /q %directory%%%~nxF
)
)
goto :eof
:NewPrint
cd "%directory%"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
if !diff! LEQ %AgeNumb% (echo.%%~nxF is !diff! days old
)
)
goto :eof
:NewDelete
cd "%directory%"
call:jdate tnow "%date%"
for %%F in (*.*) do (
call:ftime tfile "%%F"
set /a diff=tnow-tfile
if !diff! LEQ %AgeNumb% (del /f /q %directory%%%~nxF
)
)
goto :eof
:ftime JD filename attr -- returns the file time in julian days
:: -- JD [out] - valref file time in julian days
:: -- attr [in,opt] - time field to be used, creation/last-access/last-write, see 'dir /?', i.e. /tc, /ta, /tw, default is /tw
:$created 20060101 :$changed 20090322 :$categories DateAndTime
:$source http://www.dostips.com
SETLOCAL
set file=%~2
set attr=%~3
if not defined attr (call:jdate JD "- %~t2"
) ELSE (for /f %%a in ('"dir %attr% /-c "%file%"|findstr "^^[0-9]""') do call:jdate JD "%%a")
( ENDLOCAL & REM RETURN VALUES
IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
)
EXIT /b
:jdate JD DateStr -- converts a date string to julian day number with respect to regional date format
:: -- JD [out,opt] - julian days
:: -- DateStr [in,opt] - date string, e.g. "03/31/2006" or "Fri 03/31/2006" or "31.3.2006"
:$reference http://groups.google.com/group/alt.msdos.batch.nt/browse_frm/thread/a0c34d593e782e94/50ed3430b6446af8#50ed3430b6446af8
:$created 20060101 :$changed 20080219 :$changed 20110316
:$source http://www.dostips.com :$Modified by Cleptography
SETLOCAL
set DateStr=%~2&if "%~2"=="" set DateStr=%date%
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('"echo.|date"') do (
for /f "tokens=1-3 delims=/.- " %%A in ("%DateStr:* =%") do (
set %%a=%%A&set %%b=%%B&set %%c=%%C))
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
ENDLOCAL & IF "%~1" NEQ "" (SET %~1=%JD%) ELSE (echo.%JD%)
EXIT /b
