Author Posts

January 1, 2012 at 12:00 am

by mk.maddin at 2013-01-04 02:37:24

Hey powershell community,

I tried to use get-childitem with the following syntax:

$Picattribs = "*.jpg", "*.jpeg", "*.gif", "*.tiff", "*.bmp", "*.png"
$Path = "$env:UserProfile" + "\Pictures"

Get-childitem $Path -Include $Picattribs -Force

This doesn't work.
Changing the line to:
Get-childitem $Path -Include $Picattribs -Force -Recurse
it works perfectly...but i don't want to have it recurse.
Running [code get-help Get-childitem -examples[/code] there is exactly what i need in Example 3 but this doesn't work as well.

Is there something set up wrong with my powershell or is this a common problem? – Could someone test, please?
If this is a common problem? How can I do a workaround?
With just one include parameter i could use this:
Get-childitem -Path $Path -Force | Where-Object {$_.Extension -eq "*.jpg"}
But like you can see above I need to have more than one parameter.

Thank you for help

mk.maddin

by nohandle at 2013-01-04 04:25:32

The example 3 works for me you have to retain the '\*' on the end of the line. Why it works this way even without the recurse parameter I don't know. Looks like a 'bug' -> if you use this behaviour make sure you document it in the code.

If you need to filter only one extension use the Filter paramter that works always.

by nohandle at 2013-01-04 04:34:35

[quote="nohandle"] Why it works this way even without the recurse parameter I don't know. Looks like a 'bug' -> if you use this behaviour make sure you document it in the code.
[/quote]
Turns out it is not a bug at all, the behavour is documented here: http://stackoverflow.com/questions/7907 ... tem-cmdlet

by mk.maddin at 2013-01-04 04:58:21

Okay,

I tested again.
Adding line characters \* at the end of the path solves the problem.
$Path = $Path + "\*"

Here the whole saved line again:
$Picattribs = "*.jpg", "*.jpeg", "*.gif", "*.tiff", "*.bmp", "*.png"
$Path = "$env:UserProfile" + "\Pictures"

$Path = $Path + "\*"

Get-childitem $Path -Include $Picattribs -Force

I don't know why this works..But i hope my workaround is helpfull for people having the same problem.

mk.maddin

by nohandle at 2013-01-04 08:26:43

Are you aware that if there is any folder named for example "test.tiff" it is going to look for the files into it? Usually there is not but you cannot be sure. Running filter for each item in the array and collecting the output is probably the fastest way. Or maybe filtering with Where-object that should be at least as quick as the include param.