【问题标题】:PowerShell Search for specific files - different results each runPowerShell 搜索特定文件 - 每次运行不同的结果
【发布时间】:2020-09-23 10:17:38
【问题描述】:

我需要在选定的文件夹和子文件夹中搜索某种类型的文件(照片和视频)。然后我必须将它们连同路径和大小一起列出。作为一个好的开始,我使用了here 提供的解决方案,但是当我通过lit(更新版本)执行脚本时,它会返回每个文件 4 次(每个文件都有不同的哈希) - 实际上,只有一个文件磁盘上。此外,当我再次执行此操作时,同一文件的哈希值不同。我做错了什么?

【问题讨论】:

    标签: powershell search


    【解决方案1】:

    您可以使用-Include 参数让Get-ChildItem 返回具有给定扩展名集的所有文件。这仅在您还指定 -Recurse 开关或路径以 \* 结尾时才有效。

    $rootFolder = 'D:\Test'     # change this to the path of your folder
    $extensions = '*.jpg', '*.jpeg', '*.png', '*.bmp', '*.mp4', '*.mkv', '*.mts'  # add more extensions if need be
    
    $result = Get-ChildItem -Path $rootFolder -File -Recurse -Include $extensions | ForEach-Object {
        # output an object with properties you need
        [PsCustomObject]@{
            File = $_.FullName
            Hash = (Get-FileHash -Path $_.FullName -Algorithm MD5).Hash  # choose another algorithm if you don't like MD5
        }
    }
    
    # output to console
    $result | Format-Table -AutoSize
    
    # output to CSV file
    $result | Export-Csv -Path (Join-Path -Path $rootFolder -ChildPath 'hashcodes.csv') -UseCulture -NoTypeInformation
    

    附注对象(例如 FileInfo 对象)上的 GetHashCode() 方法不是确定文件的文件哈希的好方法。为具有与 Int32 类型相同或更小的范围的数值计算哈希码的最简单方法之一是简单地返回该值。最好使用Get-FileHash cmdlet

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-19
      • 2021-08-12
      • 2017-03-06
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 2011-08-07
      • 1970-01-01
      相关资源
      最近更新 更多