How to Remove Files/Folder Older than X days in Windows using Command Prompt


Often, we need to clean up files and folders that are older than x number of days before they eat up all the disk space.  So we need a script/command to delete the old files/folder.


   1: @echo off

   2: :: set folder path

   3: set target_path=C:\Users\xxxx\AppData\Local\Temp


   5: :: set min age of files and folders to delete

   6: set max_days=7


   8: :: remove files from %target_path%

   9: forfiles -p %target_path% -m *.* -d -%max_days% -c "cmd  /c del /q @path"


  11: :: remove sub directories from %dump_path%

  12: forfiles -p %target_path% -d -%max_days% -c "cmd /c IF @isdir == TRUE rd /S /Q @path"




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