How To Clean Up Old Windows Files
Windows 10 has a number of improvements that allow you to keep your PC clean. Its recent versions include built-in tools to automatically empty the Recycle Bin and clean the Downloads folder periodically. Unfortunately, if you are running an early build of Windows 10 or a previous version of the OS, these features are not available for you. Here is how you can delete files older than certain days using three different methods.
To achieve this goal, you don't even need any third-party tools. This can be done using either File Explorer, PowerShell or a batch file.
File Explorer is the default file manager app in Windows 10. It has a special search box. When it gets focused, it shows a number of advanced options in the Ribbon. To activate the search feature in File Explorer, click on the search box or press F3 on the keyboard. The Ribbon will then look as follows:
Here is how to use it to delete files older than a certain number of days.
Delete Files Older Than X Days with File Explorer
- Open the Search tools tab in the Ribbon (F3).
- Click on the Date modified button. It has a drop down list with options.
- Select the desired option, like Last week.
File Explorer will filter the results immediately. Select the files you don't need, and press the Delete key to delete files. Alternatively, you can right-click the selection and choose Delete from the context menu.
Tip: You can use your own, custom size filters. All you need is to type the desired filter condition in the search box of File Explorer as follows:
datemodified:11/1/2017 .. 11/20/2017
Instead of 'datemodified', you could use 'datecreated' to find files in a specific date range.
Alternatively, you can type the date related parameter and enter a colon character (:). This will make File Explorer show the date picker. Pick a date or condition from the calendar pop-up. You can click on a date and drag to specify a date range. This way you can filter the results to get exactly what you want.
This method is good for deleting files manually once in a while. If you need to automate the procedure, e.g. clean up the Downloads folder on a periodic basis, you should use either the command prompt or PowerShell methods. Let's review them.
Delete Files Older Than X Days with a Batch File
In my previous article, Find Large Files in Windows 10 Without Third-Party Tools, we have learned about the useful ForFiles console command. This command selects a file (or a set of files) and executes a command on that file.
The switches we can use are as follows:
/S - This switch makes forfiles recurse subdirectories. Like "DIR /S".
/D - Select files with a last modified date. For example,-365 means over a year ago, -30 means a month ago.
/P - To indicate the path to start the search.
/C "command" - This command specifies the command to execute on each file that will be found. Command strings should be wrapped in double quotes.
The default command is "cmd /c echo @file".
The following variables can be used in the command string:
@file - returns the name of the file.
@fname - returns the file name without extension.
@ext - returns only the extension of the file.
@path - returns the full path of the file.
@relpath - returns the relative path of the file.
@isdir - returns "TRUE" if a file type is
a directory, and "FALSE" for files.
@fsize - returns the size of the file in bytes.
@fdate - returns the last modified date of the file.
@ftime - returns the last modified time of the file.
To delete files older that X days, do the following.
- Open a new command prompt instance.
- Type the following command:
ForFiles /p "C:\My Folder" /s /d -30 /c "cmd /c del @file"
Substitute the folder path and the amount of days with desired values and you are done.
For example, to remove the files older than a month from the Downloads folder, use the following command:
ForFiles /p "%userprofile%\Downloads" /s /d -30 /c "cmd /c del @file"
This trick works in all modern versions of Windows including Windows 7, Windows 8, Windows 8.1 and Windows 10.
Delete Files Older Than X Days Automatically
You can automate this task using the built-in Task Scheduler app.
- Open Administrative tools and click on the Task Scheduler icon.
- In the left pane, click the item "Task Scheduler Library":
- In the right pane, click on the link "Create task":
- A new window titled "Create Task" will be opened. On the "General" tab, specify the name of the task. Pick an easily recognizable name like "Delete Old Files".
- Switch to the "Actions" tab. There, click the "New..." button:
- The "New Action" window will be opened. There, you need to specify the following data.
Action: Start a program
Program/script:ForFiles.exe
Add arguments(optional):/p "%userprofile%\Downloads" /s /d -30 /c "cmd /c del @file"
Change the folder path and the number of days to what you need. - Go to the Triggers tab in your task. There, click on the New button.
- Under Begin the task, select "On a schedule" in the drop down list and click the OK button. Specify when you want the task to run.
- Switch to the "Settings" tab. Enable the options
- Allow task to be run on demand.
- Run task as soon as possible after a scheduled start missed.
- Click OK to create your task.
That's it.
Finally, if your prefer PowerShell, you can use a special cmdlet to remove old files.
Delete Files Older Than X Days with PowerShell
- Open a new PowerShell window.
- Type the following command:
Get-ChildItem "%userprofile%\Downloads" -Recurse | Where-Object {($_.LastWriteTime -lt (Get-Date).AddDays(-30))}| Remove-Item
If the Get-ChildItem cmdlet finds any files that are older than a month, then the Remove-Item cmdlet will be called for each file to remove it.
That's it.
Support us
Winaero greatly relies on your support. You can help the site keep bringing you interesting and useful content and software by using these options:
If you like this article, please share it using the buttons below. It won't take a lot from you, but it will help us grow. Thanks for your support!
How To Clean Up Old Windows Files
Source: https://winaero.com/delete-files-older-x-days/
Posted by: twiggsdayssiders67.blogspot.com

0 Response to "How To Clean Up Old Windows Files"
Post a Comment