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

Jaden· Answered on Apr 16, 2024

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

Can't Delete PDF Files from A Shared Folder, What Can I Do

Shift + Delete can be used to delete PDF files or directories permanently. If you still can't remove PDF files on a Windows server, read how to force delete a PDF file that won't be deleted on your Windows PC.

Jaden

Jun 20, 2024

Is It Impossible to Completely Delete Files from the Computer

When you delete a file from your computer, it does not simply vanish - at least not immediately. The data may be recoverable. This guide will show you how to permanently delete files so they cannot be recovered in Windows 11/10/8/7.

Jaden

Jun 20, 2024

How to Fix 'Can't Delete a File on Windows Server' Issue

It will sometimes happen that you cannot access or delete a specific file suddenly. We think the methods below will help you get out of this situation, and you can have a try.

Dany

Jun 20, 2024

Quickly Delete Massive File Structure on Windows Server 2012

Normally, the deletion of large files takes a long time. Your computer needs to confirm and analyze the data, which will take time. The methods below may help you quickly delete massive files structure on Windows Server 2012.

Dany

Jun 20, 2024

Recover Accidentally Deleted Game Files from Steam

If you accidentally deleted your Steam game files, for example, when cleaning storage and accidentally deleting game files, don't worry! This article will tell you practical ways to recover accidentally deleted Steam game files.

Brithny

Jun 20, 2024

How to Recover Deleted Files from SD Card Using Chrome OS

SD cards are widely used due to their high data transfer speed and hot-swappable features, although various data loss issues plague them. Don't panic if you accidentally delete files from your SD card; different recovery methods and SD recovery software can help you retrieve deleted files from your SD card.

Jaden

Jun 20, 2024