使用PowerShell进行递归文件搜索


238

我正在所有文件夹中搜索文件。

Copyforbuild.bat 可在许多地方使用,我想进行递归搜索。

$File = "V:\Myfolder\**\*.CopyForbuild.bat"

如何在PowerShell中执行此操作?

Answers:


393

Get-ChildItem cmdlet与-Recurse开关一起使用:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force

1
似乎有一个问题,如果它运行到您没有访问权限的目录中,则整个搜索将中止,因为该过程将退出。有办法解决吗?
deed02392

6
尝试将ErrorAction参数设置为Continue或SilentlyContinue(以防其值未提及)。
Shay Levy

22
一种做相同事情的更短方法: cd V:\MyFolder其次ls -r -inc CopyForBuild.bat
Orion Edwards

8
因此,以上评论者的意思是……ls -r -ea silentlycontinue -fo -inc "filename.txt" | % { $_.fullname }
安德鲁

我通过*在文件名的末尾添加星号()来使此答案适合部分字符串。****** LINE1:$File = "Microsoft.OData.Core.NetFX35.V7*"LINE2:$Folder = "C:\Program Files"LINE3:Get-ChildItem -Path $Folder -Filter $File -Recurse -ErrorAction SilentlyContinue -Force
SherlockSpreadsheets

37

我用它来查找文件,然后让PowerShell显示结果的完整路径:

dir -Path C:\FolderName -Filter FileName.fileExtension -Recurse | %{$_.FullName}

您始终可以*FolderName和/或中使用通配符FileName.fileExtension。例如:

dir -Path C:\Folder* -Filter File*.file* -Recurse | %{$_.FullName}

上面的示例将搜索C:\驱动器中以单词开头的任何文件夹Folder。因此,如果您有一个名为的文件夹FolderFooFolderBarPowerShell将显示这两个文件夹的结果。

文件名和文件扩展名也是如此。如果要搜索具有特定扩展名的文件,但不知道文件名,则可以使用:

dir -Path C:\FolderName -Filter *.fileExtension -Recurse | %{$_.FullName}

或相反亦然:

dir -Path C:\FolderName -Filter FileName.* -Recurse | %{$_.FullName}

4
我发现这个答案非常有用,因为它解决了特定的用例-实际上是想使用您要查找的文件。| 为您提供文件的全名是其他答案中缺少的内容。
Newteq Developer

1
我必须在这里同意@ Sanity1123,如果您实际上要使用该文件,则需要该文件的完整路径。恕我直言,这应该是公认的答案。
uceumern

31

搜索文件夹时,可能会基于安全性而出错(例如C:\Users),请使用以下命令:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse -ErrorAction SilentlyContinue -Force

12
Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat

也会起作用


如果要将结果输出到文件,则可以在命令末尾添加`> path_to_filename.txt`。
Gwen Au

7

这是我在奋斗之后终于想出的方法:

Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*

要使输出更整洁(仅路径),请使用:

(Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname

要仅获得第一个结果,请使用:

(Get-ChildItem -Recurse -Path path/with/wildc*rds/ -Include file.*).fullname | Select -First 1

现在了解重要内容:

仅搜索文件/目录,请不要使用-File-Directory(请参阅下面的原因)。而是将其用于文件:

Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}

并删除-eq $falsefor目录。千万不能让像一个尾随通配符bin/*

为什么不使用内置开关?它们很糟糕,会随机删除功能。例如,为了-Include与文件一起使用,必须以通配符结尾路径。但是,这将禁用该-Recurse开关,而不会告诉您:

Get-ChildItem -File -Recurse -Path ./bin/* -Include *.lib

您可能认为这会给您所有*.lib子目录中的全部,但只会搜索的顶级bin

为了搜索目录,可以使用-Directory,但是随后必须删除尾随的通配符。无论出于何种原因,这都不会停用-Recurse。出于这些原因,我建议不要使用内置标志。

您可以大大缩短此命令:

Get-ChildItem -Recurse -Path ./path*/ -Include name* | where {$_.PSIsContainer -eq $false}

变成

gci './path*/' -s -Include 'name*' | where {$_.PSIsContainer -eq $false}
  • Get-ChildItem 别名为 gci
  • -Path 默认位置为0,因此您可以设置第一个参数路径
  • -Recurse 别名为 -s
  • -Include 没有速记
  • 在名称/路径中的空格中使用单引号,以便您可以在整个命令中用双引号引起来,并在命令提示符中使用它。反之(用单引号引起来)会导致错误

6

试试这个:

Get-ChildItem -Path V:\Myfolder -Filter CopyForbuild.bat -Recurse | Where-Object { $_.Attributes -ne "Directory"}

2
您还可以使用| Where-Object { !$_PSIsContainer }排除文件夹
Gargravarr

5

使用通配符过滤:

Get-ChildItem -Filter CopyForBuild* -Include *.bat,*.cmd -Exclude *.old.cmd,*.old.bat -Recurse

使用正则表达式进行过滤:

Get-ChildItem -Path "V:\Myfolder" -Recurse
| Where-Object { $_.Name -match '\ACopyForBuild\.[(bat)|(cmd)]\Z' }

1
不是'\ACopyForBuild\.(bat|cmd)\Z'吗?
E. Sundin

1

要添加到@ user3303020答案并将搜索结果输出到文件中,可以运行

Get-ChildItem V:\MyFolder -name -recurse *.CopyForbuild.bat > path_to_results_filename.txt

这样可能更容易搜索正确的文件。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.