【问题标题】:Difference between -include and -filter in get-childitemget-childitem 中 -include 和 -filter 的区别
【发布时间】:2015-02-19 07:40:59
【问题描述】:

有人能解释一下Get-ChildItem 命令中-Include-Filter 选项之间的区别吗?

以下是我尝试执行的两段代码。它们都用于查找特定目录中的文本文件:

PS C:\Users\352997> get-childitem -path Desktop\Extras -filter *.txt


    Directory: C:\Users\352997\Desktop\Extras


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---        12/22/2014   4:05 PM        140 Expense_report.txt
-a---         1/14/2015   4:41 PM        211 Extras.txt
-a---         2/10/2015   2:46 PM        259 Learn Dutch.txt

PS C:\Users\352997> get-childitem -path Desktop\Extras -include *.txt

--上面的命令没有结果----

【问题讨论】:

    标签: powershell


    【解决方案1】:
    1. Filter 参数由提供者实现。它很有效,因为在检索对象时应用。 Get-PSprovider commandlet 显示实现“过滤器”参数的提供程序。例如,我的网站上只有两个提供商 系统:ActiveDirectory 和文件系统

    2. Include参数由Powershell实现。它只能与 Recurse 参数结合使用(如 MSDN 描述的 here)。

    3. 有趣的是:

      get-childitem -path Desktop\Extras\ -include *.txt
      

      什么都不返回

      get-childitem -path Desktop\Extras\* -include *.txt
      

      返回 *.txt 文件列表

    也许这些只是实现的细微差别。

    另请参阅这篇出色的博文:http://tfl09.blogspot.com/2012/02/get-childitem-and-theinclude-and-filter.html

    【讨论】:

    • 关于你的要点3:如docs.microsoft.com/en-us/powershell/module/…所述,'-Include 参数仅在命令包含-Recurse 参数或路径指向目录的内容时有效,例如如 C:\Windows*,其中“*”通配符指定 C:\Windows 目录的内容。'
    • 那么使用-include-filter 有什么优势吗?我开始怀疑我是否应该完全放弃-include,因为-filter 级别较低,并且不依赖于-recurse-path .\*。为什么-include 存在?
    • @jschmitter - 这实际上听起来像是另一个(也许很好)的问题 - 搜索重复项,如果找不到,请询问 :)
    • 除了性能之外的区别还在于功能 -filter 只允许 * 和 ?而-include 允许使用正则表达式。因此,这实际上取决于您要过滤的内容和数量。它们也可以组合使用,因此您可能会获得 一些 -Filter 性能。
    • @Stoinov -include 不使用正则表达式,我希望这样做
    【解决方案2】:

    -filter 应该比-include 快。 -filter 可以匹配 powershell 5.1 中文件名的短版本。

    【讨论】:

      猜你喜欢
      • 2019-02-17
      • 1970-01-01
      • 2010-12-05
      • 1970-01-01
      • 1970-01-01
      • 2013-06-03
      • 1970-01-01
      • 1970-01-01
      • 2011-03-14
      相关资源
      最近更新 更多