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.
- 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:
2. Save the notepad file with .bat extension
3. Test the batch file by opening it
Before running the batch file. I had 3 Text files:
After running the batch file, only two files were left as 3 days old file got deleted
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 the scheduled task runs the batch file.
- What is Active Directory? A Complete Guide for IT Professionals - 18 November 2024
- Enterprise Virtualization Platforms Comparison: A Technical Deep Dive - 29 October 2024
- Docker Swarm: The Complete Guide to Container Orchestration - 28 October 2024