【发布时间】:2020-09-23 10:17:38
【问题描述】:
我需要在选定的文件夹和子文件夹中搜索某种类型的文件(照片和视频)。然后我必须将它们连同路径和大小一起列出。作为一个好的开始,我使用了here 提供的解决方案,但是当我通过lit(更新版本)执行脚本时,它会返回每个文件 4 次(每个文件都有不同的哈希) - 实际上,只有一个文件磁盘上。此外,当我再次执行此操作时,同一文件的哈希值不同。我做错了什么?
【问题讨论】:
标签: powershell search
我需要在选定的文件夹和子文件夹中搜索某种类型的文件(照片和视频)。然后我必须将它们连同路径和大小一起列出。作为一个好的开始,我使用了here 提供的解决方案,但是当我通过lit(更新版本)执行脚本时,它会返回每个文件 4 次(每个文件都有不同的哈希) - 实际上,只有一个文件磁盘上。此外,当我再次执行此操作时,同一文件的哈希值不同。我做错了什么?
【问题讨论】:
标签: powershell search
您可以使用-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
【讨论】: