繁体   English   中英

使用 PowerShell 搜索包含两次或多次出现的特定字符串的文件名

[英]Search for filenames containing two or more occurrence of a specific string using PowerShell

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

编辑:

这是我到目前为止所拥有的:

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

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

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

伟大的!

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

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

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

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*"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM