【发布时间】:2017-06-04 20:44:10
【问题描述】:
我正在编写一个脚本来识别路径中所有文件的哈希值(并且递归地)。没关系。
我的问题是,在我确定哪些哈希相同后,我想将它们保存到一个数组中,以便稍后我可以删除这些具有相同哈希的文件(如果我愿意的话),或者只打印重复的文件。我整个下午和晚上都在试图弄清楚如何去做。 我现在的代码:
Write-Host "Write a path: "
$UserInput=Read-Host
Get-ChildItem -Path $UserInput -Recurse
#Get-FileHash cmdlet to get the hashes
$files = Get-ChildItem -Path $UserInput -Recurse | where { !$_.PSIsContainer }
$files | % {(Get-FileHash -Path $_.FullName -Algorithm MD5)}
#Creating an array for all the values and an array for the duplicates
$originals=@()
$copies=@()
#grouping the hashes that are duplicated cmdlet Group-Object:
$Duplicates = Get-ChildItem -Path $UserInput -Recurse -File |Group {($_|Get-FileHash).Hash} |Where Count -gt 1
foreach($FileGroup in $Duplicates)
{
Write-Host "These files share hash : $($FileGroup.Name)"
$FileGroup.Group.FullName |Write-Host
$copies+=$Duplicates
}
所以最后一部分“$copies+=$Duplicates”不能正常工作。
一开始我想把第一个文件保存在“原始”数组中。如果第二个具有相同的散列,则将第二个保存在“副本”数组中。但是我不确定在获取哈希值时是否可以在脚本的第一部分执行此操作。
之后,第二个数组会有重复的,所以很容易从计算机中删除它们。
【问题讨论】:
标签: powershell hash