How to Delete Files and Folders with Exclusion Circumstances

I'm looking for a script to delete files and directories that haven't been updated in the last three days from the E: disk drive. The script should be run as a task scheduler and not destroy files or directories ending, and it should also omit E:WMP directories. I've been hunting for the script but haven't found it.

Best Answered by

Dany· Answered on Mar 10, 2023

Using PowerShell, you can directly exclude or include particular files that do not satisfy criteria in the deletion process.  You can delete files older than X days from your computer using Storage Sense and the Windows Command Prompt.

First, you need to define your dates for deletion. To automatically delete files older than X days in Windows 11/10 using Storage Sense, follow these steps:

  • Press "Win+I" to open Windows Settings.
  • Select System - Storage and then Storage Sense.
  • Toggle the Automatic User Content Cleanup button.
  • Choose a setting from the Run Storage Sense menu.
  • Click the button to launch Storage Sense.

Then set your exclusions. In Command Prompt, Use a file exclusion range to exclude files from a DEL command. Delete all files in the current directory except those with the extension, for example. TXT, use the following command:

del /[!*.TXT] *

When using exclusion ranges or other more sophisticated parameters, you may wish to use the N switch first to see how the DEL works without actually deleting any files.

Unless the option is supplied, DEL displays the amount of disk space reclaimed. It compares the amount of free disk space before and after executing the DEL command. If you use a deletion tracking system that maintains deleted files in a hidden directory or another software performs a file operation while the DEL command runs, this number may be wrong.

Keep in mind that DEL destroys both file descriptions and files. Even if they can save or recover the data in a file, most deletion tracking systems will not be able to save or recover the file's description. This is also true when using DEL with the Windows Recycle Bin - the description will be lost. Then, how to solve the lost description? The four most commonly used solutions - Attrib CMD (Command Prompt), CMD tool, Windows Control Panel, and EaseUS Data Recovery Wizard to show hidden files on a Windows computer are available here to help.

Use EaseUS Data Recovery Wizard to recover deleted or lost files easily and quickly.

Tutorials on how to recover lost, deleted, or formatted data from HDD, SSD, USB, SD card, or other storage media on Windows and Mac.

People Also Ask

How to Recover Deleted/Unsaved Smart Notebook Files [Windows Tips]

It's typical on the internet for people to desire to get their hands on Smart Notebook files. You should read this article if your smart notebook has ceased functioning or if you have lost the data on it.


Feb 27, 2023

How to Fix Can't Delete a File That's Stuck on Desktop

There are several methods to delete a file that is stuck on your desktop, and we will show you some possible solutions to help you troubleshoot this problem in this passage.


Feb 22, 2023

Does Format USB Drive Delete Everything

When your USB drive has some issues or is corrupted, there will be a notice to tell you that your memory card needs to be formatted. Before formatting, you can first recover data from this memory card and troubleshoot the memory card issue.


Mar 09, 2023

Difference Between Delete and Shift Delete

For a new Windows user, using delete shortcuts to delete files is very convenient. If you don't know the difference between Delete and Shift Delete, we will introduce both of them to you.


Feb 22, 2023

How to Delete a File/Folder at Logon [GPO Script Settings]

It is not a good thing that agents and supervisors can check some sensitive information. We will help you use the logon/logoff script to solve the issue in this passage.


Feb 22, 2023

What Is the Use of Shift Delete on Windows

We know many shortcuts can be used on Windows, and we are going to introduce the use of Shift + Delete on Windows. We will also let you know when and why you need to use this shortcut on Windows in this passage.


Mar 09, 2023