ForFiles – Delete files older than X amount of Days

Windows 2003 – ForFiles

FORFILES /P C:\MyPath\ /S /M log*.xml /D -30 /C "cmd /c del @file"

/S – look at subdirectories

log*.xml – that is the file pattern, you could have it be like *.txt for example

/D -30 = files greater than 30 days

/C – thats the command you want to run

this is pretty much a one line command that could replace a ton of vbScripts, batch files, and other apps I have seen over the years to clean up files. Something for every sys admin’s toolbelt.



  • http://blog.super-networking.net Chris

    Thanks for the info!

  • http://www.moshiur.com ERROR

    I am using the forfiles.exe version:5.2.3790.0 (srv03_rtm.030324-2048) in windowsXP.
    I used your(ForFiles – Delete files older than X amount of Days Feb.27, 2007 in Geeky/Programming)above command and found it didn’t work in my computer but when I tested the following command in batch file
    ————————————————-

    @ECHO OFF
    ECHO ******www.moshiur.com***********
    forfiles /p C:\test22\test2\ /s /m *.* /d -3 /c “cmd /c del echo @FILE”
    EXIT

    ————————————————-

    I found that it was working fine including files in sub directories in my given path but thing is that it couldn’t delete any subdirectories. Do you have advice ?

  • http://blog.stevienova.com Steve Novoselac

    mine in the post works, it is just that wordpress screws up the “quotes” so when you copy/paste it , it doesnt work.

    if you want to remove directories, try the cmd line “rd”

    such as rd /s /q myfolder

  • Wolfsrudel

    The “DEL” command only deletes files not folders. For folders you have to use “RMDIR”. :)

  • Mark H

    Steve,

    Thank you so much for the info on FORFILES!!

    Its exactly what I was looking for and it works great!!

    Thanks again.

    Mark

  • http://twitter.com/LuisAngelPerez Luis Angel Perez

    Teste and Working. Thanks very much!!!

  • Praveen

    Hi,
    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:forfiles.exe and tried to run forfiles command from command promt, but no success.
    Thanks

  • Praveen

    Hi,
    I tried to running this in my system(WIN-XP operating system), for some reason it is NOT deleting any files.
    I didn't had forfiles.exe in my system, I downloaded from the net and placed it in D:forfiles.exe and tried to run forfiles command from command promt, but no success.
    Thanks

  • Phil

    Any secret on file names and folders with spaces and, numbers and periods in them?

  • ric

    I have a problem where it prompts me to delete folders that have “.” in the folder name. For example foldername.06.27.2010
    Is there any way to suppress that? it stalls the script until you answer yes or no.

  • http://blog.stevienova.com Steve

    try with quotes around?

  • Bobh

    On Server 2003, I am getting the error that the '/C' option is only allowed once. Any ideas

  • Nobody

    add _/q_ right after the _del_ …

  • Andy_vincent

    Not sure if you’ve resolved this. I had the same problem and it was due to me copying the text from above. The quotes were formatted. re-type the quotes in dos etc and it should work.

  • Ayk

    Use For instead forfiles for XP

  • Bob

    The problem I am experiencing is that the forfiles batch file acts as it should for 5 days (does not delete any files as I am using /D -5), but once a single file gets to 5 days old, all the files in the directory are deleted – including those only 1, 2, 3 or 4 days old at the time.

    FORFILES /P “B:NovaBackups” /S /M *.nbd /D -5 /C “CMD /C DEL /F /Q @FILE”

    Any ideas on what I might try differently?

    TIA

  • Bob

    Well I guess I was just having a senior DUH moment!  The batch file runs at night as do the daily backups – batch file first and then backup run.  When the first file hits >5 days old it is deleted.  But the backup program considers that file to be the first in a set, even though each backup run is a complete backup and not an incremental/differential run after a full backup.  So since the backup program doesn’t see what it considers to be the requisite 1st backup in a set, it creates the 1st backup in what it considers a new series and deletes the remaining backups in the old set.  Since the batch file and  backup run happen sequentially, and the only place I knew there was a delete command was in the forfiles batch file, I assumed I had done something wrong with that.  I figured I needed to come clean and save anyone spending time on my brain fart.

  • Mitch Gent

    I’m having the same issue, I suspect it fines a file that’s 5 days old, and runs the del *.bak command on the folder, I’d suspect you’d need to specify the ‘date of the file in the actual delete statement as well. Anybody run into this? I set it up to delete backups after 3 days, and every 3 days, it removes them all.

  • Mitch Gent

    I think this fixed it
    after the
    forfiles /p “F:BackupsDailyBLAH” /m *.bak /d -3 /c  “cmd /c if @fdate>=3 del *.bak”  >> E:elitedbMSSQLTaskLogDeleteFromBK.log 
    This also pipes output to the folder.

  • SteveO

    Works perfect on win2k8 r2. A handy one for deleting folders rather than just files:  ForFiles /P “C:mytopdirectory”  /D -1 /C “CMD /C if @ISDIR==TRUE echo RD /S /Q @FILE &RD /S /Q @FILE”

  • X-DraGoN .

    Good info right there, using it now, thanks!

  • Zaidner

    thanks  works great