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

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

问题描述

来自文档:

-包括

仅检索指定的项目.此参数的值符合条件路径参数.输入路径元素或模式,例如*.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.

Include 参数只有在命令包括 Recurse 参数或路径通向 a 的内容目录,例如 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:	esta.txt
c:	est.txt

所以为了得到 'a.txt' 和 'b.txt' 我可以这样写:

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

gci -Path "c:	est*" -Include "*.txt"

这是有效的.但现在考虑这样的层次结构:

And this works. But now consider such hierarchy:

c:	esta.txt
c:	est.txt
c:	estc.txtc.txt

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

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

实际的逻辑似乎是:

-Include 用于匹配-Path 指定的所有实体.如果匹配元素是一个文件 - 返回它.如果匹配element 是一个文件夹,往里面看返回匹配的一级子级.

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

另外,文档说:

Include 参数只有在命令包括 Recurse 参数或路径通向 a 的内容目录...

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:	est" -Include "*.txt"

它什么都不返回,而没有 -Include 我得到文件夹内容.所以 -Include 绝对是有效的".这里到底发生了什么?-Path 指定c: est",而 -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: est", and the -Include tries to match this path. As "*.txt" does not match "test", so nothing returned. But look at this:

gci -Path "c:	est" -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: est 在路径中有一个 t,因此匹配 "*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: est has a t in the path and hence matches "*t".

您可以通过尝试以下方法来验证这一点

You can verify this by trying the following

gci -path "c:	est" -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天全站免登陆