The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

lysanth (1) [Avatar] Offline
#1
I would like to spin through a directory and it's subdirectories in order to remove files of a certain extensions. I would like to just run the command and forget about it until it's done. So far I have:

get-childitem c:TestFiles -include *.txt, *.log, *.obj, *.pdb -recurse | remove-item -whatif

I have a few questions: is there a limit to the number of extensions I can include? My script above is an example but I would like to include about 70 or 80 file extensions. Also, do I need to start with the get-childitem cmd or can I just find and remove the files (by using remove-item)? Is it more efficient to use -filter or -include in this case? Thank you very much in advance for your help.
nicktf (2) [Avatar] Offline
#2
Re: Using Powershell to find and remove selected files
According to the detailed help for remove-item, the recurse parameter doesn't work, so you will have to use get-childitem in the meantime. It doesn't appear to be fixed in v2.
jhammer (5) [Avatar] Offline
#3
Re: Using Powershell to find and remove selected files
Hi lysanth,

This sounded like a python script I wrote about a year back, so your post has prompted my interest into taking a look at the PowerShell version.

The -include parameter for Get-ChildItem takes in an array of strings, which is an indication to me that it has been written to function no matter how many extensions you throw at it. I put together a test of my own and it didn't have any trouble running through the 100 extensions I passed it.

I was looking into the -include vs. -filter issue and unfortunately I can't tell who is going to be faster based on the help documentation:

-include:
Retrieves only the specified items. ...

-filter:
... Filters are more efficient than other parameters, because the provider applies them when retrieving the objects, rather than having Windows PowerShell filter the objects after they are retrieved.

It sounds to me that they would accomplish the same thing in the same way. I may be wrong about this-- so I'm going to second your question:

Is it more efficient to use -filter or -include in this case?

However, it should be noted that -filter only takes in a single string as opposed to -include's array of strings. So to me it sounds like -include is what we're looking for in terms of passing a list of extensions to run through.

Thank you!

- Joey