How to delete older files using command prompt

How to delete older files using command prompt

How to delete older files using command prompt

We usually get the requirement to automatically delete the files in regular intervals, we can achieve this task by creating a batch file and create a schedule task to delete files automatically in regular Intervals.

  1. Open a Notepad and put the below command:

forfiles -p “Folder location” -s -m *.* /d -<No. of days> /C “cmd /c del @path” 

(Replace the folder location with complete folder path where you need to delete underneath files.  Specify the days after /d if you want to specify how many days older files shall be deleted. You can also add extension to delete specific files.For Example: forfiles -p

“C:\Users\ravi.chopra\Downloads” -s -m *.txt* /d -3 /C “cmd /c del @path”)

Here I want to delete text files in my download folder and have set the 3 days older files should be deleted:

How to delete older files using command prompt

2. Save the notepad file with .bat extension

files2

3.  Test the batch file by opening it

Before running the batch file. I had 3 Text files:

files3

After running the batch file, only two files were left as 3 days old file got deleted

files4

4.     Add this batch file to Schedule Task: Open a Schedule Tasks Console à Click on Create Basic Task and follow the wizard put the batch file & schedule the time to run the task.

You are done! The files shall be automatically deleted whenever schedule task would run the batch file.

Scroll to Top