The ForFiles command tool comes to the rescue

Feb 1, 2018 13:15 GMT  ·  By

Microsoft is introducing options in Windows 10 Redstone 4 that will allow users to automatically clean the downloads folder and Recycle Bin at regular intervals, but for users who want to remove files without manual input from other directories, third-party solutions are the only solution.

Fortunately, the built-in Windows tools come to the rescue and actually allow you to do just that: remove files older than a certain number of days from a specific folder in a second.

Basically, here’s how the whole thing works before jumping right into details. The commands that you can find below help you build a script (a BAT file) that will automatically look for files older than a number of days defined by you and which would then be removed, leaving the directory clean.

What you need to know is that depending on the folder that you want to clean, administrator privileges could be needed, though it most of the cases, standard users should be able to run these commands just fine.

WHO NEEDS IT? The answer to this question is as simple as it could be: anyone who wants to keep folders clean, like the downloads library, can turn to such scripts to automatically delete folder items. On the other hand, system admins and IT pros managing systems in a larger network are the ones that could find this more helpful than others, as it makes cleaning a breeze without the need for third-party software.

WHY WE NEED IT? Keeping files that you don’t need on your system not only that eats up valuable space, but it also slows down loading that specific folder, so the more often you clean the computer, the better the performance. Also, this tool is recommended to all those who want to perform maintenance regularly.

WHEN WE NEED IT? This depends on how often you want to remove older files from your directories. For example, if you want to remove items older than 30 days from the downloads folder, it’s enough to run the script below every month and you should be good to go.

Now that we answered the basic questions, let’s proceed with the essentials. For this task, we’re going to use the ForFiles command tool whose purpose is to specifically manage files from the command line utility. ForFiles support several parameters and I’ll discuss the ones we’ll use below.

For this guide, we’re using the default Windows 10 downloads folder located at C:\Users\Username\Downloads and the 30 days target as settings, but you can change them to whatever you want.

Running the cleaning command in PowerShell

#1. Launch Command Prompt/Windows PowerShell (with administrator privileges) by right-clicking the Windows 10 Start menu or by typing their names in the Start menu.

#2. The standard command that we’re going to use here is the following (I used italic to highlight the areas that you need to change according to your settings - the path represents the folder that you want to clean and 30 is the number of days):

ForFiles /p “C:\Users\Username\Downloads” /s /d -30 /c “cmd /c del @file” The meaning of this command is the following:  
/p represents the path of the folder that you want ForFiles to look in for files older than a specific number of days;
/s means that ForFiles will apply the removal rule in sub-directories as well, should any be there;
/d is the indicator for the date, in our case 30 days
/c sends ForFiles the command to execute the task
del stands for delete
@file represents the name of the file and in this command, it applies the command to all files
At first glance, running this particular command is nothing too difficult, though it goes without saying that you need to manually input the path every single time. Fortunately, you can simplify it a little bit.
Opening PowerShell in a certain folder

Navigate to the folder that you want to clean, open it, press SHIFT and then right-click in the folder. Click the option that reads Open PowerShell window here (or Command Prompt if you’re on an older version of Windows) and enter the following command:

ForFiles /s /d -30 /c “cmd /c del @file” In this example, we removed the /p (path) parameter because we no longer need it since we’re already in the folder that we need to clean.

There are several other ways to automate the cleaning process of each folder, and you can do it with a .BAT file that includes the following lines:

@echo off
ForFiles /s /d -30 /c “cmd /c del @file”
end
With this script, all files older than 30 days (including the script itself) will be removed when simply launching the BAT file.
Building a script to automatically configure cleaning

Photo Gallery (4 Images)

This guide uses ForFiles to execute the cleaning
Running the cleaning command in PowerShellBuilding a script to automatically configure cleaning
+1more