【问题标题】:powershell - search subdirectories for multiple filespowershell - 搜索多个文件的子目录
【发布时间】:2021-01-04 11:16:55
【问题描述】:

我有一个存储在文本文件中的文件名列表,我想搜索特定目录和子目录并获取所有存在的文件的完整路径。

现在我正在使用下面的代码

$FileListPath = "C:\FileList.txt"
$SearchPath = "Y:\Project"
foreach($FileName in Get-Content $FileListPath){(Get-ChildItem -Path $SearchPath -Recurse -ErrorAction SilentlyContinue -Include $FileName).FullName}

这工作正常,但速度很慢。我认为因为对于每个文件,它都会一次又一次地进行完整搜索。有没有更快的方法来做到这一点?

另外,这不是绝对必要的,但我想排除包含 obj 作为文件夹之一的子文件夹。例如: Y:\folder\ 我想被包含,但 y:\Folder\obj\ 和 Y:\Folder\obj\subfolder 我想被排除在外。

谢谢

【问题讨论】:

    标签: powershell search


    【解决方案1】:

    -Include 参数采用文件名的字符串数组,因此您可以这样做:

    $FileListPath = "C:\FileList.txt"
    $SearchPath   = "Y:\Project"
    $exclude      = 'obj'
    $fileNames = Get-Content $FileListPath
    
    (Get-ChildItem -Path $SearchPath -Recurse -File -Include $fileNames -ErrorAction SilentlyContinue | 
     Where-Object{ $_.DirectoryName -notmatch $exclude }).FullName
    

    如果您要跳过多个子文件夹,请将$exclude 设置为:

    $exclude = ('obj','skipthis','not_this_one' | ForEach-Object { [Regex]::Escape($_) }) -join '|'
    

    【讨论】:

      猜你喜欢
      • 2022-01-02
      • 1970-01-01
      • 1970-01-01
      • 2014-06-13
      • 2021-05-25
      • 2021-06-20
      • 2012-11-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多