Powershell command for deleting everything EXCEPT for... Solved

  1.    #1

    Powershell command for deleting everything EXCEPT for...


    Hey guys, just wondered if anyone could help me with this.

    So basically, I have a directory with a crap ton of subdirectories, each with a crap ton of files in them. The files all have widely varying names (they're all ZIP files so they have the same extension), and I only want to keep the files with a certain string in the filename.

    What command would allow me to recursively delete every file except for ones containing this string? Is this even possible via command prompt, or perhaps there's a GUI application that could do it?
      My ComputerSystem Spec

  2.    #2

    I would use xcopy to copy the files you want: xcopy source_path\*<string>* dest_path /s/i
    and once you are happy they have been copied, delete the originals. I don't know of a way to selectively delete files that don't fit a spec'n, but testing it would be fun (not). Type xcopy /? at the command line to see what the various /switches do.

    hth, Martin
      My ComputerSystem Spec

  3.    #3

    Copying could work, the only reason I wasn't initially considering that is because the disk that I'm doing this operation on is roughly getting near to fullness and the data I'm operating on is fairly large, so if I could accomplish this by deleting instead of copying then that would be beneficial. I'll see if I can get away with copying in the meantime, though!

    EDIT: Also, copying wouldn't work very well because the subdirectories don't have the specific string in them.
      My ComputerSystem Spec

  4. Kari's Avatar
    Posts : 15,654
    Windows 10 Pro
       #4

    This is quite easy in File Explorer.

    Let's say you'd like to delete each and every ZIP file (extension .zip) in a certain folder and all its subfolders, except those with string TenForums anywhere in the filename.

    Open Explorer in folder in question, and type following in Explorer Search:

    ext:zip name:~!TenForums

    This search will find and list all files with extension .zip, except those where the string TenForums appears in filename. Now just select all, and delete.

    Search parameter name:~! followed by any string tells Search to exclude each and every file with that string in filename from search.

    Extremely easy, no copying back and forth required.

    Kari
      My ComputerSystem Spec

  5.    #5

    Here's a PS solution

    Edit: The second command is the correct one - dunno why I upped a test screenshot (been a long day... LOL)


    Before:

    Click image for larger version. 

Name:	Screenshot from 2019-02-22 18-30-30.png 
Views:	8 
Size:	184.0 KB 
ID:	225175

    After:

    Click image for larger version. 

Name:	Screenshot from 2019-02-22 18-32-05.png 
Views:	8 
Size:	132.1 KB 
ID:	225176
    Last edited by Superfly; 4 Weeks Ago at 12:04.
      My ComputerSystem Spec

  6.    #6

    CursedLemon said: View Post
    EDIT: Also, copying wouldn't work very well because the subdirectories don't have the specific string in them.
    If the other suggestions work, then fine, but I just tested my simple solution and it creates the sub-folders, regardless of their name, if any files need to be copied into them.

    Just tried Kari's explorer suggestion and its nifty!
      My ComputerSystem Spec

  7. Kari's Avatar
    Posts : 15,654
    Windows 10 Pro
       #7

    mngerhold said: View Post
    If the other suggestions work, then fine, but I just tested my simple solution and it creates the sub-folders, regardless of their name, if any files need to be copied into them.
    Both File Explorer and PowerShell solutions offered will work, without copying certain files to another location, then back after the remaing files in original location have been deleted. In addition, in both of those solutions, no time is needed for copying files.

    Kari
      My ComputerSystem Spec

  8.    #8

    I went with the explorer option, if only because I needed to do some extra culling after I performed the first deletion which was much easier to keep going at in GUI form, and it worked out great. Thanks everyone for the suggestions! I bet they'll all come in handy in different instances. :)
      My ComputerSystem Spec


 

Related Threads
178957178956 The screen shots show results of running "C:\Program Files\AVAST Software\Avast\ashCmd.exe" /@=Quick in both PS and Cmd. The problem is I have made a task in Task Scheduler to run this command daily and, apparently, Task Scheduler...
How do you change from PowerShell to Command Prompt using file explorer ?
Hello, I'm looking for a way to check PC's IP Geolocation by using command prompt or powershell (no scripts). I hope there are some experts here who can help me out. I have found a very easy to do it on UNIX systems but my main machines will be...
:really:I have a few PCs connected via a bridge on an ethernet LAN. They communicate with each other, not "the internet". I'd like to rename "Unidentified network" to something more reasonable like "DoNotTreadOnMe" (allegorical.) 76590
I've encounter that all of my windows 10 apps cannot launch. I click them in the start menu but without response. I've executed powershell command Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register...
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

Designer Media Ltd
All times are GMT -5. The time now is 18:56.
Find Us