【问题标题】:Search for filenames containing two or more occurrence of a specific string using PowerShell使用 PowerShell 搜索包含两次或多次出现的特定字符串的文件名
【发布时间】:2021-06-20 09:23:44
【问题描述】:

我想打印目录中所有名称中多次包含特定字符串的文件(而不是文件本身)。有没有办法用 Powershell 做到这一点?

编辑:

这是我目前所拥有的:

Get-ChildItem -Path "path" -Recurse -Filter *string*

有了这个,我得到所有包含该字符串的文件至少一次,但我只需要它出现两次或更多的文件。

【问题讨论】:

  • 是的,这当然是可能的。到目前为止,您尝试过什么?
  • 很简单,我们可以看看您在代码中遇到的问题吗?
  • 我只有这个:Get-ChildItem -Path "path" -Recurse -Filter *string* 这给了我所有包含该字符串至少一次的文件,但我只需要它出现两次或更多次的文件。
  • 请在您的问题中添加此信息,包括代码 sn-p。提前致谢。

标签: string windows powershell search filenames


【解决方案1】:

我只有这个:Get-ChildItem -Path "path" -Recurse -Filter *string*

太棒了!

现在你需要做的就是重复这个词:

Get-ChildItem -Path "path" -Recurse -Filter *string*string*

如果有问题的子字符串有空格,你需要引用字符串:

Get-ChildItem -Path "path" -Recurse -Filter '*looking for this*looking for this*'

如果字符串包含通配符文字(如[]),您可以像这样对其进行转义:

$escapedString = [wildcardpattern]::Escape("string [ with special ] characters")

所以整个事情变成了:

$substring = "string we're [looking] for"
$searchTerm = [wildcardpattern]::Escape($substring)
Get-ChildItem -Path "path" -Recurse -Filter "*$searchTerm*$searchTerm*"

【讨论】:

  • 好吧,这比我想象的要简单 ^^' 谢谢!
  • @DonCan94 当你知道的时候,你就知道了 ;-) 不客气!
猜你喜欢
  • 2013-05-06
  • 2020-12-30
  • 2018-10-02
  • 1970-01-01
  • 2018-10-18
  • 1970-01-01
  • 1970-01-01
  • 2021-07-14
  • 2014-11-18
相关资源
最近更新 更多