How to Delete All Files That Do Not Match Criteria

I have a folder full of reports. Each week, more reports are posted. I'd like a weekly script that deletes all files in all subfolders if the end of the file name (except the extension) does not match a predefined phrase. Any files that do not finish '07.02.16' OR '31.01.16' should be destroyed, for example. Please help! Thank you very much.

Best Answered by

Dany· Answered on Feb 03, 2023

Using PowerShell allows you to explicitly exclude or include specific files that do not match criteria in the deletion process. The following cmdlet example explains removing files with the filename *.LOG.

One method is to use the Remove-Item cmdlet directly using the -Include argument, as illustrated below:

  • Remove-Item -Path C:\temp\* -Include *.log

Another method, possibly the most conservative, is to use the Get-ChildItem cmdlet first to gather the list of files to be destroyed. You can finally pipe the collection to the Remove-Item cmdlet once you're satisfied with the list of files to delete. For instance, the code below gets that *.LOG files in c:\temp.

  • Get-ChildItem -Path C:\temp\* -Include *.log

The Get-ChildItem produces a list of files that match the *.LOG filename as a result of the code above. But what if you wish to exclude files with the number 07.02.16 in their names? You can impart the -Exclude argument, as shown in the following code:

  • Get-ChildItem -Path C:\temp\* -Include *.log -Exclude *07.02.16*

Because you excluded the file with the number 07.02.16, the outcome has changed. The file File 07.02.16.log, in particular, is no longer on the list.

  • Get-ChildItem -Path C:\temp\* -Include *.log -Exclude *07.02.16* | Remove-Item -Verbose

When you're through with the collection of files your code generates, pass the collection to the Remove-Item cmdlet to finally destroy those files. When you run your final code, you will have accomplished your goal of deleting only the files you specified.

Create a script that will execute proper disk space for you. Sure, some scripts for that purpose already exist; by all means, use them as a reference, but if you're prepared to practice and learn, you should strive to write your own.

If any of your files become corrupted while the Command Prompt script is running, you can learn how to delete corrupted files using a variety of approaches. However, if you enter the wrong path and accidentally delete some important files, you should immediately stop using your PC and utilize a file recovery application to recover them. EaseUS Data Recovery Wizard is a powerful software that can recover deleted files.

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 Fix Zip Folder Cannot Be Deleted on My Computer

Many reasons can cause this error, and we will provide several available plans to help you get out of this problem. Keep reading to learn the details.

Dany

Jan 30, 2023

How to Fix 'Windows 10 Administrator Can't Delete Unwanted Folder on USB'

How to fix 'Windows 10 administrator can't delete a folder on USB'? We will provide multiple solutions to help you resolve this problem. Keep reading and find an answer to your question!

Dany

Jan 12, 2023

How to Create A PowerShell Script to Delete Contents of Shared Folders

This post will explain how to delete single and many files using the Remove-item cmdlet and how to delete files using WMI. Also, if you have mistakenly erased files, EaseUS Data Recovery Wizard can recover them with 99.9% accuracy.

Dany

Jan 10, 2023

How to Delete Files and Folders with Exclusion Circumstances

Do you want to learn how to remove a file with an exclusion circumstance using PowerShell? Don't panic because this answer will show you how to delete numerous files with the Remove-item cmdlet and delete files that do not match the criteria.

Dany

Jan 12, 2023

How to Delete Aimed Folder Contents Larger Than 500MB

When dealing with huge directories, deleting a folder and its contents using Windows Explorer might take a lifetime because it deletes each file one by one. The quickest method is to delete the folder from the command prompt on Windows.

Dany

Feb 03, 2023

How to Restore Deleted Folder from Virtual Machines in Unraid

The Unraid user is periodically bothered by the virtual machines' data recovery and the virtual machines' disk folders not found issues. This article provides methods for recovering data from the disk of a virtual machine to minimize the loss of a virtual machine's disk file.

Dany

Dec 27, 2022