Skip to main content

Batch file to delete files older than x days

··274 words·2 mins

At work, I have bunch of batch files which take an export from Oracle database, compress them and move them to their respective folders. In addition to this, the scripts also copy the Oracle forms executibles (“fmx”), reports (“rdf”) and other miscellaneous files to the backup locations and these are further compressed. The compressed files are then transferred over to the SAN.

Now the problem was that these backup files would still remain on the hard drive, littering the folders and resulting in waste of space - I was looking for an alternative to get rid of these files via a scripted approach. The catch is that this is a Windows server and I’m restricted to the CLI that Windows provides - and no PowerShell either. So while researching for ways to select files older than x days, I came across forfiles command on Stack Overflow (God bless thee).

forfiles: Select a file (or set of files) and execute a command on each file.

Perfect. Exactly what I needed. After reading the help file and experimenting with it, I rolled out a script which will delete any file older than 30 days.

@echo off
@Echo Deleting files older than 30 days...

forfiles /p g:\backup /d -30 /c "cmd /c del @file"

So this will basically search for any file(s) older than 30 days ( based on the Last Modification Date attribute) in the path g:\backup and will execute del @file where del == delete, @file is the variable which holds the filename.

You can further filter out the files by using /m switch. I’d recommend you have a look at the description over here.