How To Delete Files Automatically From Windows

Delete Files Automatically

To Delete files from window (Delete Files Automatically), you have to create a batch file and schedule it to run. In this article you will learn step by step to create batch file and schedule it to run on recurring basis.  

Create Batch File:

To create batch file first open notepad and write the given line of text on notepad just copy paste it on notepad.

forfiles -p "C:\Test" -s -m *.* /D -5 /C "cmd /c del @path"

The above line is basically tells the window to delete all files from C:\Temp folder and sub folders that are older than 5 days. -s denote that the command should look into all sub folders also to delete and -m *.* denotes specific file type in a folder like .pdf, .jpg, .txt etc. if you don’t want then remove -s or -m from command.

Proceeding next now we have to save the file and run it for testing and follow the below steps-

  • Create a folder named Temp at the C drive root directory.
  • Click on the file to save as a batch file, the extension of the file will be .bat.
  • Change the “Save as type” to open to “All files”.
  • Create some of dummy files in the temp folder and click on the .bat file to run to delete the files but because of the command has /D -5, denote the 5 days older file will only delete, you have to remove or change the /D -5 to /D -0, and again run the file. Now it will delete the files from C:\Temp.
  • Alternatively if you want to change the directory other than mentioned in notepad, you can copy the path from window Explorer and paste it on the notepad.

You can add more than one command in a .bat file that will delete all type of folders (Delete Files Automatically) that you mentioned in file.

Schedule Batch File To Run:

To run the .bat file on recurring basis we have to open Task Scheduler from PC startup and change the trigger accordingly, as daily, weekly or monthly and also you can configure the exact time.

Hope this article will be helpful to perform the file delete task for you, thanks.