Command line to delete every file older than 30 days


I found a new command line directive i’d never heard of today, along with a brilliant example of it’s use- once you have changed your working directory, running this will remove any files or folders older than 30 days;

FORFILES /D -30 /C "CMD /C IF @isdir==FALSE (echo Deleting File @file) & (ATTRIB -H @file) & (DEL /F /Q @file) ELSE (echo Deleting Directory @file) & (ATTRIB -H @FILE) & (RD /S /Q @FILE)"

As an example, I’ve since used this to iterate over a bunch of folders, deleting any files over 30 days;

d:
for /f "delims=|" %%f in ('dir /b D:\_SQL_Backup\') DO (
  cd \_SQL_Backup
  cd %%f
  FORFILES /D -14 /C "CMD /C IF @isdir==FALSE (echo Deleting File @file) & (ATTRIB -H @file) & (DEL /F /Q @file)"
)
  1. No comments yet.
(will not be published)