Windows 10 has some in-build features like Task Scheduler and as everyone is familiar with Command Prompt, in this article I will explain both approaches, how to purge files using task scheduler, and how to purge files with command prompt after certain days.
Why purging is important?
Mail purpose of purging is to clean up stored files that consume drive space which makes the system slow in performance or you have some sensitive files which need to be cleaned up after certain days for security purposes.
Task Scheduler
The Task Scheduler is a Windows component that can automatically run tasks at a specific time or in response to a specific event. For example, tasks can be run when the computer starts or when a user logs in. You can start tasks when a specific event occurs and using a task scheduler you can run commands and execute scripts on a specific day and time.
Command Prompt
In the Windows operating system, the Command Prompt is a program that emulates the input field in a text-based user interface screen with the Windows Graphical User Interface (GUI). It can be used to execute entered commands and perform advanced administrative functions.
So here are the key points will discuss in this post:
- Purge files older than 10 days using Task Scheduler
- Purge files older than 10 days using Command Prompt
Purge files older than 10 days using Task Scheduler
To automate the purging process using Task Scheduler, you need to create a task that executes commands at given intervals. You need to follow these steps:
Open start and search for Task Scheduler and click on it.
Right-click on Task Scheduler Library and click on New Folder and give the folder name (PurgeFilesProcess) and click OK.
Right-click on the newly created folder and select the Create Task option.
In General tab, provide the task name and select "Run whether user is logged on or not" in the Security options, also clear the "Do not store password" option.
Note - If you are using a service account on servers then change that service account name in the Change user or Group option.
Next, click on Triggers tab and click on New button. Provide schedule according to your need, I am setting up the trigger in every 10 days, means this task will run after 10 days on given time and click OK.
Click on the Actions tab and click New button. And select Start a program from Action.
Enter ForFiles command into the Program/script box and add the following command in Add arguments (optional)
/p "C:\path\to\folder" /s /d -10 /c "cmd /c del /q @file"
You can change this like this:
/p "C:\TestFiles" /s /d -10 /c "cmd /c del /q @file"
C:\TestFiles is the directory where files are stored.
Click the OK button.
Click on the Settings tab and make sure to select the given options:
- Allow task to be run on demand.
- Run task as soon as possible after a scheduled start is missed.
- If the task fails, restart every and click OK button.
After doing all the above settings the task is scheduled to run to delete the files for the given number of days.
Important
Before moving next, let’s discuss ForFiles command arguments:
- /p - represents the pathname to start searching.
- /s - represent searching inside subdirectories.
- /d - represents the last modified date for a file.
- /c - instructs ForFiles to execute the command (must be wrapped in double quotes). The default is “cmd /c del @file.”
- /q - represents deleting folders without requiring confirmation.
Purge files older than 10 days using Command Prompt
This is another way to purge the files using the command prompt, the only difference is you cannot schedule a task using the command prompt, you need to run the command every time you need to purge the files. Follow these steps:
Open start on the window and search for Command Prompt and click Run as administrator.
Enter the following command which deleted the files on the windows folder which are 10 days older and click enter.
ForFiles /p "C:\path\to\folder" /s /d -30 /c "cmd /c del /q @file"
Update the command based on your need and press enter.
ForFiles /p "C:\TestFiles" /s /d -10 /c "cmd /c del /q @file"
Conclusion
In this article, we have learned how to create a file purge process using Windows 10 Task Scheduler and command prompt.