Powershell Get-ChildItem -Filter 与具有相同值的 Where 子句的操作不同 [英] Powershell Get-ChildItem -Filter operates differently to Where clause with same value

查看:23
本文介绍了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:SampleMyFolder.*

我将在未来轻松使用此语法以减少代码中的噪音.

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 子句的操作不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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