views:

632

answers:

4

From documentation:

-Include

Retrieves only the specified items. The value of this parameter qualifies the Path parameter. Enter a path element or pattern, such as "*.txt". Wildcards are permitted.

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.

My first understanding was:

c:\test\a.txt
c:\test\b.txt

So to get 'a.txt' and 'b.txt' I can write:

gci -Path "c:\test\*" -Include "*.txt"

And this works. But now consider such hierarchy:

c:\test\a.txt
c:\test\b.txt
c:\test\c.txt\c.txt

The same command returns: a.txt, b.txt, c.txt

The actual logic seems to be:

-Include used to match all entities specified by -Path. If matched element is a file - return it. If matched element is a folder, look inside and return matching first level children.

Also, the documentation say:

The Include parameter is effective only when the command includes the Recurse parameter or the path leads to the contents of a directory...

This is wrong as well. E.g.

gci -Path "c:\test" -Include "*.txt"

It returns nothing, while without -Include I get folder content. So -Include is definitely "effective". What really happens here? The -Path specify the "c:\test", and the -Include tries to match this path. As "*.txt" does not match "test", so nothing returned. But look at this:

gci -Path "c:\test" -Include "*t"

It returns a.txt, b.txt and c.txt as "*t" matched "test" and matched all child items.

After all, even knowing how Include works now, I don't understand when to use it. Why do I need it look to inside subfolders? Why should it be so complex?

+3  A: 

You're confusing the use of -include. The -include flag is applied to the path, not the contents of the path. Without the use of the recursive flag, the only path that is in question is the path you specify. This is why the last example you gave works, the path c:\test has a t in the path and hence matches "*t".

You can verify this by trying the following

gci -path "c:\test" -in *e*

This will still produce all of the children in the directory yet it matches none of their names.

The reason that -include is more effective with the recurse parameter is that you end up applying the wildcard against every path in the hierarchy.

JaredPar
I am using PowerShell-v2.0-CTP3, and your example produces empty set.
alex2k8
@alex2k8, Not sure what to say here. I've tried this example on 1.0 and a later 2.0 build, both produce the same results.
JaredPar
@Jared, this is strange. Please, can you try gci -path c:\* -in *. I see children of the c:\ and all it's subfolders one level down. Here is the similar description http://www.eggheadcafe.com/software/aspnet/32624832/getchilditem-include-qu.aspx
alex2k8
+2  A: 

Tacking on to JaredPar's answer, in order to do pattern matching with Get-ChildItem, you can use common shell wildcards.

For example:

get-childitem "c:\test\t?st.txt"

where the "?" is a wildcard matching any one character or

get-childitem "c:\test\*.txt"

which will match any file name ending in ".txt".

This should get you the "simpler" behavior you were looking for.

Steven Murawski
Isn't the issue here that you can only get a single extension? -Include is string[] allowing for multiple matching variations.Thanks, Ben
Ben
+3  A: 

Try the -filter parameter (it has support for only one extension):

dir -filter *.txt

Shay Levy
Filter is slightly different because it is a provider specific implementation. It's typically more efficient because it occurs before the data is returned. But it does have a different implementation for every drive provider in PowerShell. Hence there can be subtle differences in the matching algorithm.
JaredPar
I agree, though in this case we're talking about the file system provider. Thnaks for the comment.
Shay Levy
A: 

Hey, I just asked a similar question and got three quick replies concerning the get-help for get-childitem.

The answer is in the full description of the command (get-help get-childitem -full):

The Include parameter is effective only when the command includes the

Recurse parameter or the path leads to the contents of a directory, such as C:\Windows*, where the wildcard character specifies the contents of the C:\Windows directory.

So the following would work without recurse.

PS C:\foo> Get-childitem -path "c:\foo*" -Include *.txt

http://stackoverflow.com/questions/1370210/ I hope this helps :-)

Ben
Ben, actually my question was a bit different. Notice, that your example will return .txt files from the foo folder, BUT it can also unexpectedly return files and even folders from for example "c:\foo\dir.txt" directory. Try such hierarchy: c:\foo\dir.txt\file.txt and c:\foo\dir.txt\dir2.txt\file2.txt. You'll get: file.txt and dir2.txt!
alex2k8