混淆了Get-ChildItem cmdlet的-Include参数 [英] Confused with -Include parameter of the Get-ChildItem cmdlet

查看:140
本文介绍了混淆了Get-ChildItem cmdlet的-Include参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自文档:


-Include

-Include

仅检索指定的项目。
此参数的值使
符合Path参数。输入路径
元素或模式,例如* .txt。
允许使用通配符。

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.

仅当命令
包含Recurse参数或
路径指向
目录的内容时,Include参数才有效,例如C:\ Windows *,其中
通配符指定C:\ Windows目录的
内容。

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.

我的第一个理解是:

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

所以要获得'a.txt'和'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

相同的命令返回:
a.txt,b.txt,c.txt

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

实际逻辑似乎是:


-Include用于匹配-Path指定的所有实体。如果匹配的元素
是一个文件 - 返回它。如果匹配
元素是一个文件夹,请查看内部并且
返回匹配的第一级子项。

-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.

此外,文档说:


仅当命令
包含Recurse参数或
路径引导时,Include参数才有效到
目录的内容...

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

这也是错误的。例如

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

它什么都不返回,而没有-Include我得到文件夹内容。所以 - 包含绝对是有效的。这里到底发生了什么? -Path指定c:\ test,-Include尝试匹配此路径。由于* .txt与test不匹配,因此没有返回任何内容。但是看看这个:

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"

它返回a.txt,b.txt和c.txt作为* t匹配test并匹配所有孩子项目。

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

毕竟,即使知道Include如何运作,我也不明白何时使用它。为什么我需要它来查看子文件夹内?为什么它应该如此复杂?

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?

推荐答案

你混淆了使用-include。 -include标志应用于路径,而不是路径的内容。如果不使用递归标志,则唯一有问题的路径是您指定的路径。这就是为什么你给出的最后一个例子,路径 c:\ test 在路径中有,因此匹配* t

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.

-include对recurse参数更有效的原因是你最终对每个路径应用通配符层次结构。

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.

这篇关于混淆了Get-ChildItem cmdlet的-Include参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆