PowerShell - 在网络共享上搜索文件时 Get-ChildItem 太慢

标签 powershell

这是我花了整整一周的代码,当然是在 stackoverflow 的帮助下按名称搜索文件,除了 Get-ChildItem 在搜索我们拥有超过 150k 文件的网络共享文件夹时似乎非常慢之外,它工作正常.

有没有更好的写法?先谢谢你。

$fileName = Read-Host -Prompt 'Enter a File Name'

# Set the path to the folder you want to search
$folderPath = '\\networkshare'

if ($pdfFile = Get-ChildItem -Path $folderPath -Filter "*$fileName*.pdf" -File -Recurse )
{
    & {
        for ($i = 0; $i -lt $pdfFile.Count; $i++)
        {
            [pscustomobject]@{
                $('#' + (' ' * $pdfFile.Count.Length)) = $i + 1
                PDFName = '{0}' -f $pdfFile[$i].BaseName
                Created = '{0}' -f $pdfFile[$i].CreationTime
            }
        }
    } | Out-String -Stream
    $selection = (Read-Host -Prompt 'Enter PDF(s) to open') -split ',' | %'Trim'
    foreach ($selected in $selection)
    {
        $pdf = $pdfFile[$selected - 1]
        Start-Process -FilePath "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe" $pdf.FullName
        Write-Host $pdf
        pause 10
    }
}
else
{
    Write-Host -Object "No file with name `"$fileName`" found."
}

我的代码只要求用户输入他们要查找的文件(这些文件都只是 PDF 文件)。 代码的作用示例。

Enter a File Name: 1234

Returns files simular to what the user entered

#  PDFName           Created
-- -------           -------
 1 TK1234            12/14/2022 6:19:45 AM 
 2 TK5467-5001234215 12/24/2022 10:57:36 AM

这在我的 c:\temp 文件夹上测试效果很好,但是当我尝试我们的网络共享时,需要很长时间才能获得结果。

最佳答案

使用 Queue<T> 会更快遍历目录而不是 Get-ChildItem .至于选择, Out-GridView -PassThru 允许选择多个项目并且已经是内置的,不需要额外的逻辑。

$filter = '*{0}*.pdf' -f (Read-Host -Prompt 'Enter a File Name')
$queue  = [Collections.Generic.Queue[IO.DirectoryInfo]]::new()

$folderPath = '\\networkshare'
$queue.Enqueue((Get-Item $folderPath))

$result = while($queue.Count) {
    try {
        $target = $queue.Dequeue()
        foreach($item in $target.EnumerateDirectories()) {
            $queue.Enqueue($item)
        }
        foreach($item in $target.EnumerateFiles($filter)) {
            [pscustomobject]@{
                Created      = $item.CreationTime
                Name         = $item.Name
                AbsolutePath = $item.FullName
            }
        }
    }
    catch {
        # ignore inaccessible / access denied folders
        # can add error handling here if needed else leave blank
    }
}

if($result) {
    $edgePath = "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"
    $result | Out-GridView -PassThru -Title 'Enter PDF(s) to open' | ForEach-Object {
        Start-Process -FilePath $edgePath -ArgumentList "`"$($_.AbsolutePath)`""
    }
}
else {
    Write-Host ('No file with name "{0}" found.' -f $filter)
}

关于PowerShell - 在网络共享上搜索文件时 Get-ChildItem 太慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/74923499/

相关文章:

mysql - 将数据表转换/处理为自定义 PSObject 的最快方法

powershell - 如何解锁/卸载Powershell创建的文件?

powershell - 如何通过powershell dsc安装visual studio 2013

git - 分支名称不符合git标准:refs/heads/master

.Net 秒表和 Powershell

Powershell,尝试仅输出目录的路径和最后写入时间

powershell - 使用参数测量启动进程的运行时间

Powershell 根据 powershell 版本完全删除命令行标志

powershell - 为什么这个 XPath 可以在 PowerShell 中工作,但不能在任务计划程序中工作?

.net - Powershell表单布局