如何在 PowerShell Get-ChildItem 的通配符模式中使用方括号? [英] How do I use square brackets in a wildcard pattern in PowerShell Get-ChildItem?

查看:85
本文介绍了如何在 PowerShell Get-ChildItem 的通配符模式中使用方括号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想列出所有以方括号中的一些文本结尾的文件.

I want to list all files ending with some text in square brackets.

但是 Get-ChildItem *[*Get-ChildItem *`[*Get-ChildItem *``[* 都不起作用.

But neither Get-ChildItem *[* nor Get-ChildItem *`[* nor Get-ChildItem *``[* work.

如何轻松完成这项工作(即通过创建变量、通过管道运行其他命令等)

How can I make this work without much ado (i.e. by creating variables, running additional commands through the pipe etc.)

推荐答案

以下,其中包括您尝试过的其中一项操作,应该有效,但是 目前[1]由于错误而无法工作:

The following, which includes one of the things you tried, should work, but currently[1] doesn't work due to a bug:

# SHOULD work, but CURRENTLY BROKEN:
Get-ChildItem *``[*    # 1st ` is for string parsing, 2nd ` for wildcard escaping
Get-ChildItem "*``[*"  # ditto, with double quotes
Get-ChildItem '*`[*'   # single-quoted alternative, requires only 1 `

请注意,(第一个)位置参数的使用隐式绑定到 Get-ChildItem-Path 参数.

Note that the use of a (the first) positional argument implicitly binds to Get-ChildItem's -Path parameter.

目的是让 Get-ChildItem 在参数解析后看到以下文字:*`[*,它正确地将 [ 转义为` 以便将其视为文字.

The intent is for Get-ChildItem to see the following literal after argument parsing: *`[*, which correctly escapes [ with ` in order to treat it as a literal.

顺便说一句:不加引号的 *`[* 等价于双引号的 "*`[*",结果是文字 *[*,因为 PowerShell 的字符串解析会解释 ` 并有效地将其删除.

As an aside: unquoted *`[* is equivalent to double-quoted "*`[*", which results in literal *[*, because PowerShell's string parsing interprets the ` and effectively removes it.

解决方法:

不是对 [ 字符进行转义,而是将其包含在 [...] 中,这是一个字符集表达式,这会使其按字面意思进行匹配:

Instead of escaping the [ character, enclose it in [...], a character-set expression, which causes it to be matched literally:

Get-ChildItem *[[]*  # OK

有趣的是,通过 -Include 执行过滤不会显示错误:

Interestingly, performing the filtering via -Include does not exhibit the bug:

Get-ChildItem * -Include '*`[*'  # OK

另一种选择是使用 -Filter 而不是(隐含的)-Path,如 Paxz 的答案,但请注意 -Filter 的通配符语言与 PowerShell 的相同(如-Path-Include/-Exclude 参数支持);-Filter 参数传递给 Windows API,其通配符语言如下不同:

Another option is to use -Filter instead of (implied) -Path, as demonstrated in Paxz's answer, but note that -Filter's wildcard language is not the same as PowerShell's (as supported by the -Path and -Include / -Exclude parameters); the -Filter argument is passed to the Windows API, whose wildcard language differs as follows:

  • 它支持更少的结构,特别是没有字符集或范围 ([...]).
  • 它有一些遗留问题 - 请参阅这个答案.
  • 从好的方面来说,使用 Filter,由于在源头进行过滤,比让 PowerShell 通过(隐含的)-Path 执行过滤效果更好>-包括.
  • It supports fewer constructs, notably no character sets or ranges ([...]).
  • It has legacy quirks - see this answer.
  • On the plus side, use of Filter, due to filtering at the source, performs better than letting PowerShell do the filtering via (implied) -Path or -Include.

另一种选择是添加另一层转义,但这是不明智的,因为一旦错误修复它就会停止工作:

Yet another option would be to add another layer of escaping, but that is ill-advised, because it will stop working once the bug is fixed:

# NOT RECOMMENDED: will stop working once the bug is fixed.
Get-ChildItem '*``[*' 

<小时>

[1] 自 Windows PowerShell v5.1/PowerShell Core 6.2.0-preview.3 起

这篇关于如何在 PowerShell Get-ChildItem 的通配符模式中使用方括号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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