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 schedule task would run the batch file.
- Essential Network Ports for Active Directory, DNS, DHCP, and ADFS - 9 September 2024
- Which RSAT Contains Active Directory Users and Computers Windows 11? - 8 September 2024
- Group Managed Service Accounts (gMSA): Benefits, Creation Steps, and Detailed Guide - 8 September 2024