Sometimes you need to delete files older than n days. For example, Backups. You can do this manually, sort them by date and delete them, or you can use a script. IN in this example we will look at a script to delete files older than n days and add a task to the scheduler Windows Server 2012R2.

1.

2.

Script for deleting files older than n days

The script is actually very simple, it is created in PowerShell and it looks like this:

FORFILES /p F:\backup\ /s /m *.* /d -30 /c "CMD /c del /Q @FILE"

Where:
F:\backup\— the path in which folder we will look for files to delete
*.* — file name (mask)
/d -30— older than how many days do we need to delete files.

This is what it looks like in PowerShell:

All that remains is to complete it and that’s it.

Creating a task in the task scheduler

Everything is fine, we have a script. We can do it manually when we need to clean up our backups. But why do it manually? We will instruct the scheduler to run our script and get rid of the routine work ourselves.

So, what do we need for this?

First, let's go to " Start -> Control Panel -> Administrative Tools" and run there " Task Scheduler«.

Select the item “ Create a task«:

On the " Are common» set the name of the task, its description and the user from whom it will be executed. By default there will be the user you are logged in as. this moment into the system.

program or script

and specify the path to PowerShell:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

And in the argument we indicate the path to our script, which we created earlier:

File "c:\script\ochistkaf.ps1"

Click " OK" once again " OK»

That's it, our task is created. All that remains is to wait until our scheduled action is completed and check whether everything was configured correctly.

One of the features latest versions Windows 10 is able to keep itself clean by automatically deleting files that have become unnecessary after a certain period of time. In earlier versions of the system, in which this function has not yet been implemented, you can use command line, console PowerShell and even Conductor, although the cleaning procedure using the latter is not so convenient.

Let's consider all three options.

Delete old files using Explorer

Explorer Tape Windows 10 And 8.1 contains a tool that allows you to sort all files in a directory by modification date, type, size and some other properties. Let's say you want to delete files older than one month. Go to the folder with the files stored in it and press F3 on the keyboard. In this case, you will be switched to the search section in the Explorer feed.

Click on the icon "Date of change" and select the desired time period, in this case a month, from the drop-down list.

Explorer will immediately sort all files by given parameter, so all you have to do is select them with the mouse and send them to the Trash. The method is simple, but not particularly convenient, since everything has to be done by hand. There is much more effective method deleting old files and now we’ll look at it.

Deleting old files on the command line

To delete files from "expired" we will use the console utility ForFiles.exe, responsible for selecting an object and performing the specified action with it. The utility supports several parameters, namely:

/S- allows you to perform a recursive search.
/P- indicates the path to a section or directory.
/D- indicates the number of days since the last modification.
/C- specifies an action command on the file.

In a team ForFiles You can also specify variables that return different values, for example, @file, which returns the file name and is what we will use. For example, let's delete all files older than 10 days in the folder "Images", located at the root of the system disk.

Open the command line and run the following command:

ForFiles /p "C:\Pictures" /s /d -10 /c "cmd /c del @file"

ForFiles / p "C:\Pictures" / s / d - 10 / c "cmd /c del @file"

The example uses three keys /p (path) , /s (recursion) And /d (amount of days) , as well as the delete command del and file name variable @file. Once we press enter, ForFiles will go through the entire contents of the folder "Images", including subdirectories And will delete all files older than 10 days .

As you can see, this method is more efficient and faster, and in addition, you can automate it by creating tasks for it in the Scheduler.

Note: After executing the command, the console may display a message that a certain file cannot be found. Don't worry, it's usually hidden file database responsible for catalog settings. If it's not in the folder (and in theory it should be),ForFiles notifies the user about this.

Removing old files using PowerShell

Here, almost everything is the same as in the example with the command line, except that the syntax is different. Let's assume that neither the path to the folder with the files nor our goals have changed. Launch the console PowerShell and execute a command like this.