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 Jan 12, 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

Need a Deleted File Recovery Tool for Windows XP

While uninstalling a program on your computer or laptop, it is easy to lose files. You will need a data recovery program if you cannot find your lost files in Recycle Bin. We will recommend EaseUS Data Recovery Wizard to help you recover the deleted files on Windows XP.


Dec 29, 2022

How to Delete Files Older Than X days Automatically

Are you tired of useless files on your computer yet eating up space? Then use this simple approach to delete files older than x days using File Explorer, CMD, and PowerShell. We can also assist you in recovering accidentally lost files on Windows 11/10.


Jan 06, 2023

The File Replication Service May Delete Files, What Can I Do

The DFS file replication may delete files; thus, you can refer to this post to learn how to avoid the circumstance by correctly using the command lines and Windows PC server. And after unintentional deletion, you can recover them with the help of the EaseUS Data Recovery Wizard.


Jan 12, 2023

How to Delete All Files That Do Not Match Criteria

Do you want to learn how to remove a file using a script? Don't worry because this post will show you how to delete numerous files with the Remove-item cmdlet and how to delete files that do not match the criteria.


Jan 11, 2023

How to Delete Files Older Than 30 Days with PowerShell

You can delete files older than 30 days with PowerShell, and there are other solutions to delete these files. We will introduce all the possible ways in this passage.


Jan 06, 2023

How to Delete Subfolders and Files by GPP

You can overcome this problem by configuring a subfolder item by GPP. Using the GPP options, you can configure a subfolder to a file system object. Before creating a subfolder preference item, you should investigate the behavior of each type of action supported by this extension.


Jan 06, 2023