Powershell command for deleting everything EXCEPT for...


  1. Posts : 23
    Windows 10 Home
       #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 Computer


  2. Posts : 1,223
    W10-Pro 22H2
       #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 Computer


  3. Posts : 23
    Windows 10 Home
    Thread Starter
       #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 Computer


  4. Posts : 17,661
    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 Computer


  5. Posts : 3,453
       #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:

    Powershell command for deleting everything EXCEPT for...-screenshot-2019-02-22-18-30-30.png

    After:

    Powershell command for deleting everything EXCEPT for...-screenshot-2019-02-22-18-32-05.png
    Last edited by Superfly; 22 Feb 2019 at 12:04.
      My Computer


  6. Posts : 1,223
    W10-Pro 22H2
       #6

    CursedLemon said:
    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 Computer


  7. Posts : 17,661
    Windows 10 Pro
       #7

    mngerhold said:
    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 Computer


  8. Posts : 23
    Windows 10 Home
    Thread Starter
       #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 Computer


 

  Related Discussions
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 07:08.
Find Us




Windows 10 Forums