Howto: automatically remove files older than ‘x’ days, Part II

Earlier I had posted my .vbs script for deleting files that were more than ‘x’ days old.

I just ran across this script that accomplishes the same thing, using Windows 2003’s native forfiles comand (scroll down to step 4).

echo on

rem First Delete old SQL Backup Files

FORFILES /p C:\filename /s /m *.* /d -3 /c “CMD /C del /Q @FILE”

rem pause

Save the script with a .bat or .cmd extension and schedule it using the Windows Scheduled Tasks applet.

12 Responses to “Howto: automatically remove files older than ‘x’ days, Part II”

  1. Putinas Says:

    Excelent – this what I wanted instead of big scary vbs script 🙂

    and since I wanted to securely erase files – I just put instead of del – sdelete – nice and clean solution 🙂

  2. Howto: automatically remove files older than ‘x’ days « the back room tech Says:

    […] just added another script that automatically removes files older than ‘x’ days which uses the forfiles command […]

  3. Edwin Says:

    Thanks for making life easier.

  4. Jason Litka Says:

    Thank you for this! I’ve been trying to find a good way to remove old SQL backups and this just hit the nail on the head.

  5. de vinck Says:


  6. Mike Says:

    Hey Julie….just wanted to let you know that “fancy quotes” in your code need to be changed before the script works 🙂

    Also, is there a way to make it report out the files that it is deleting? Mine doesn’t do that. I’m using it at the top of my ntbackup script for my external harddrive….

    FORFILES /p F:\ /m *.bkf /d -16 /c “CMD /C del /Q @FILE”
    for /f “Tokens=1-4 Delims=/ ” %%i in (‘date /t’) do set dt=%%j%%k%%l-%%i
    for /f “Tokens=1” %%i in (‘time /t’) do set tm=-%%i
    set tm=%tm::=%
    set dtt=%dt%%tm%
    C:\WINDOWS\system32\ntbackup.exe backup systemstate “@C:\Documents and Settings\Administrator\Local Settings\Application Data\Microsoft\Windows NT\NTBackup\data\scripts\CD-Copy-S.bks” /n “CD-Copy-S-%dtt%” /d “CD-Copy-S-%dtt%” /v:no /r:no /rs:no /hc:off /m copy /j “CD-Copy-S” /l:s /f “F:\%dtt%.bkf”

  7. paris Says:

    forfiles command doesn’t work on windows xp.
    pls suggest


    • Julie Says:


      Just grab a copy of forfiles.exe from a Windows 2003 server and copy it to your XP workstation.


  8. Alessandro Says:

    Great script with forfiles command !

    And if I need to keep at least 3 files in every directory too ?

  9. Dalibor Says:

    big thanks for help

  10. Karl Says:

    tested on windows 7 and server 2003, thanks.

  11. DOS Junkie Says:

    In the voice of comic book guy:


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: