本文介绍了Powershell Get-ChildItem -Filter与具有相同值的Where子句的操作不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在名为MyFolder的服务器上有一个文件夹.还有其他名为MyFolder.1,MyFolder.2,MyFolder.3等的文件夹.

I have a folder on a server called MyFolder. There are additional folders called MyFolder.1, MyFolder.2, MyFolder.3 etc.

如果我跑步:

gci C:\Sample | ? { $_.Name -like "MyFolder.*" }

我得到了预期的输出:

    Directory: C:\Sample


Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----        16/10/2012     12:16            MyFolder.1                                                                
d----        16/10/2012     12:16            MyFolder.2                                                                
d----        16/10/2012     12:16            MyFolder.3  

但是如果我跑步:

gci C:\Sample -Filter "MyFolder.*"

我得到:

    Directory: C:\Sample


Mode                LastWriteTime     Length Name                                                                      
----                -------------     ------ ----                                                                      
d----        16/10/2012     12:16            MyFolder                                                                  
d----        16/10/2012     12:16            MyFolder.1                                                                
d----        16/10/2012     12:16            MyFolder.2                                                                
d----        16/10/2012     12:16            MyFolder.3                                                                

我对输出中包含 MyFolder 感到困惑.我希望输出是一样的.

I'm confused on how MyFolder is included in the output. I'd expect the output to be the same.

在线帮助突出显示了过滤器的语法基于提供程序,但是我不确定在此实例中正在使用什么提供程序.

The online help highlights that the syntax of the filter is based on the provider but I'm unsure what provider is being used in this instance.

我在这里缺少基本知识吗?我试图将正则表达式字符串传递到过滤器中,例如"MyFolder\.*",但这根本不返回任何内容.我确定我缺少一些简单的东西.

Am I missing a fundamental piece of knowledge here? I've attempted to pass a regex string in to the filter e.g "MyFolder\.*" but this simply returns nothing. I'm sure I'm missing something simple.

我正在运行Powershell版本2.

I'm running Powershell version 2.

感谢Roman Kuzmin指出了通配符匹配的差异.下面给出了预期的输出:

Thanks to Roman Kuzmin for pointing out the differences in wildcard matching. The following gives the expected output:

gci C:\Sample\MyFolder.*

将来我将使用此语法以简化代码,以减少噪声.

I'll be using this syntax for ease in the future to reduce noise in code.

推荐答案

FileSystem提供程序的Filter宁愿使用CMD通配符,也不使用PowerShell通配符. CMD通配符在某些极端情况下很有趣,而且不直观,主要是历史上的情况.这是一个有趣的解释: https://devblogs.microsoft.com/oldnewthing /20071217-00/?p = 24143

The Filter of FileSystem provider rather uses CMD wildcards than PowerShell wildcards. CMD wildcards are funny and not intuitive in some edge cases, mostly historically. Here is an interesting explanation: https://devblogs.microsoft.com/oldnewthing/20071217-00/?p=24143

另一个需要牢记的地方:ls -Filter *.txt实际上从PowerShell的角度来看类似于*.txt*之类的文件,即扩展名为开头且以 txt 开头的文件.在某些情况下,这可能是意外的,非常不愉快:)

Another gotcha to be kept in mind: ls -Filter *.txt in fact gets files like *.txt* in PowerShell sense, i.e. files with extensions starting with txt. This may be unexpected and very unpleasant in some scenarios :)

这篇关于Powershell Get-ChildItem -Filter与具有相同值的Where子句的操作不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 20:34