How to find and Delete Old Files on Windows 11 using Command Prompt

Configurare noua (How To)

Situatie

Solutie

If you prefer Command Prompt, use the ForFiles command to automatically find and delete files older than the specified number of days from select locations. You can run the command each time you want to clean up your storage.

To use it, open Windows Search, search for Command Prompt, and select “Run as Administrator”.

In the User Account Control prompt, choose “Yes”.

In Command Prompt, type the following command. Replace “FolderPath” with the full path to the folder where you want to delete files. Replace “60” with the number of days a file must have existed on your storage for it to be deleted. (Make sure to keep the – (minus sign) before the day number.)

ForFiles /p “FolderPath” /s /d -60 /c “cmd /c del /q @file” 

Here’s what each parameter does in this command:

  • ForFiles: This is the command that retrieves files using the specified criteria.
  • p: This parameter tells the command to search files in the given directory.
  • FolderPath: This is the full path to the folder where you want to delete files.
  • s: This parameter ensures the files inside the subdirectories of the specified folder are deleted.
  • d: This parameter sets the date filter, which is followed by the number of days.
  • c: This parameter runs the command given next to it.

Note that Command Prompt will delete your files quietly, meaning it won’t ask for a confirmation. And that’s it. If you want to use this command multiple times, instead of typing it each time, turn it into a batch script. This way, you simply run a file to execute the command and delete the specified files.

To do that, open Notepad, copy and paste your ForFiles command, and select File > Save As. Click the “Save as Type” drop-down menu and choose “All Files.” Select the “File Name” field, type a name for your script, add .bat at the end of the name, and click “Save”.

When you want to run your command, right-click your batch script and choose “Run as Administrator”

You can integrate this batch script with Task Manager, and Task Manager will run the script at your specified time. This basically automates the entire process of finding and removing old files from your specified folders.

Tip solutie

Permanent

Voteaza

(5 din 11 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?