简体   繁体   English

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

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

I would like to print all files of a directory which contain a specific string multiple times in their name (not in the file itself).我想打印名称中多次包含特定字符串的目录的所有文件(而不是文件本身)。 Is there a way to do this with Powershell?有没有办法用 Powershell 做到这一点?

Edit:编辑:

This is what I have so far:这是我到目前为止所拥有的:

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

With this I get all the files which contain that string at least once, but I only need the ones where it occures twice or more.有了这个,我得到所有包含该字符串的文件至少一次,但我只需要它出现两次或更多的文件。

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

Great!伟大的!

Now all you need to do is repeat the word:现在你需要做的就是重复这个词:

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

If the substring in question has spaces, you'll need to quote the string:如果有问题的 substring 有空格,则需要引用字符串:

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

If the string contains wildcard character literals (like [ or ] ), you can escape it like this:如果字符串包含通配符文字(如[] ),您可以像这样对其进行转义:

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

So the whole thing becomes:所以整个事情变成了:

$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