Sometimes it is necessary to delete files that are older than 1 day, a week, or even a year. We just don’t need them anymore, maybe they are irrelevant, take up a lot of space on our hard drive or just a by-product of another process.


Usually, it will happen with log files that are generated automatically, and after a specific time, are not useful anymore.
In my case, I have four security cameras outside my house that are constantly taking photos (every minute), and I don’t have enough disk space in order to save them all. I decided that a week back is more than what I need for my purposes so, I wanted every image that is older than 7 days in the images folder to automatically be deleted, without any intervention. My plan was to create a script as a batch file and run it every day in the Windows Task Scheduler.

Delete files older than X days from command line CMD

Create a batch file like deleteFiles.bat and put under a library like c:\Scripts\
Edit the file, and insert the code below:

Change the constants path_to_del and del_days to fit your needs and save the file

Run the delete script from the Windows Task Scheduler

Goto Start->Run and type tasksched.msc and hit Enter key.

Add a task with a time trigger to run the script we’ve just created, and we’re done! From now on, the folder that we specified will be emptied from the older and unwanted files.

A trigger in the windows scheduler

An action in the windows scheduler

The task summery

The task summery

Leave a comment