batch file to delete files older than a specified date [duplicate]

I use this script:

////////////////////////////////////////////////////////
// Deletes file older than a number of days 
// in the current directory
////////////////////////////////////////////////////////
// Usage: wscript DeleteOlderThan.js [#Days]
// By default, remove files older than 30 days
////////////////////////////////////////////////////////

function removeDays(date, nDays)
{
    var dateRet = date
    return dateRet.setDate(date.getDate() - nDays);
}

function addSlash(strPath)
{
    var c = strPath.substr(-1, 1);
    if( c !== '\\' && c !== '/' )
    {
        strPath += '\\';
    }
    return strPath;
}

// Read arguments
var nDays = WScript.Arguments(0) || 30;

// Create system objects
var fs = WScript.CreateObject("Scripting.FileSystemObject");
var shell = WScript.CreateObject("WScript.Shell");

// Retrieve current directory
var strDirectoryPath = addSlash(shell.CurrentDirectory);

// Compute date
var dateNow = new Date();
var dateTest = removeDays(dateNow, nDays);

// Iterate on files
var folder = fs.GetFolder(strDirectoryPath);
var files = folder.Files;

for( var it = new Enumerator(files); !it.atEnd(); it.moveNext() )
{
    var file = it.item();

    if( file.DateLastModified < dateTest)
    {
        file.Delete(true);
    }
}

which I invoke every day with:

wscript "C:\Program Files\Utils\DeletesOlderThan.js" 30

If you're on a Windows Server and/or have the Server Resource Kit installed, forfiles may be exactly what you're looking for.

Examples: print all file names, older than 180 days

forfiles /S /D -180 /C "cmd /C Echo @Path" >olderthan180days.txt

delete all PDF files, older than 365 days

forfiles /S /M *.pdf /D -365 /C "cmd /C Del @Path"

Edit: I figured it out.

To delete all files older than a given date:

REM del_old.bat
REM usage: del_old MM-DD-YYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> FILES_TO_KEEP.TXT
for /f "tokens=*" %%a IN ('xcopy *.* /L /I /EXCLUDE:FILES_TO_KEEP.TXT null') do if exist "%%~nxa" del "%%~nxa"

To delete all files newer than a given date:

REM del_new.bat
REM usage: del_new MM-DD-YYYY
for /f "tokens=*" %%a IN ('xcopy *.* /d:%1 /L /I null') do if exist "%%~nxa" del "%%~nxa"

eJames: I found your xcopy solution to work very well, indeed! The thing I like most about it is that it should work in Windows language versions other than English as well, since the format of the XCOPY parameters seems to be date-setting independent. Wonderful! ;]

One improvement would be to pre-create the FILES_TO_KEEP.TXT file so that if no file match the keep criteria, the second XCOPY statement can still go ahead and do the delete (it fails if it cannot find the FILES_TO_KEEP.TXT file). Here's my script (please note in this example I changed it to only delete *.pdf files and I also changed the temp file name to make more sure there are no potential conflicts, as well as cleaning up the temp file afterwards):

@echo off
SET OLDERTHAN=%1
IF NOT DEFINED OLDERTHAN GOTO SYNTAX

echo >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /d:%1 /L /I null') do if exist %%~nxa echo %%~nxa >> ~~~FILES_TO_KEEP.TXT~
for /f "tokens=*" %%a IN ('xcopy *.pdf /L /I /EXCLUDE:~~~FILES_TO_KEEP.TXT~ null') do if exist "%%~nxa" del "%%~nxa"
del ~~~FILES_TO_KEEP.TXT~

GOTO END

:SYNTAX
ECHO.
ECHO USAGE:
ECHO DELOLD mm-dd-yyyy
ECHO   Where mm-dd-yyyy is the date prior to which you want to delete files.
ECHO.
ECHO EX: "DELOLD 10-17-2008" Deletes files older than October 17, 2008.
ECHO.
ECHO This should work on any language version of Windows, but has only been 
ECHO tested in English-US versions.
GOTO END

:END