【问题标题】:Comparing hashes and delete files with same hash in powershell not working在powershell中比较哈希并删除具有相同哈希的文件不起作用
【发布时间】: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


    【解决方案1】:

    我认为您应该过滤这些项目。我做到了,我有一个只有一项重复文件的列表和一个包含所有重复文件的列表。

    您可以使用 SHA1 算法代替 MD5

    SHA1 比 MD5 算法快得多

    $fileHashes = Get-ChildItem -Path $myFilePath -Recurse -File | Get-Filehash -Algorithm SHA1
    $duplicates = $fileHashes | Group hash | ? {$_.count -gt 1} | % {$_.Group} 
    
    $uniqueItems = @{}
    $doubledItems = @()
    
    foreach($item in $duplicates) {
      
      if(-not $uniqueItems.ContainsKey($item.Hash)){
        $uniqueItems.Add($item.Hash,$item)
      }else{
        $doubledItems += $item
      }
    }
    
    # all duplicates files
    $doubledItems
    
    # Remove the duplicate files
    # $doubledItems | % {Remove-Item $_.path} -Verbose
    
    # one of the duplicate files
    $uniqueItems
    

    设置搜索根文件夹

    $myFilePath = ''
    

    【讨论】:

    • 谢谢!效果很好!例如,您将如何删除数组中的所有文件?不是删除数组也不是数组中的元素,而是实际的文件!
    • $doubledItems | % {删除项目 $_.path}
    【解决方案2】:

    您应该只需要使用一次Get-ChildItem,一旦您拥有所有文件,您就可以为它们创建一个哈希,然后对这些哈希进行分组以查找重复项。请参阅下面的示例代码:

    Write-Host "Write a path: "
    $UserInput=Read-Host
    
    #Get-FileHash cmdlet to get the hashes
    $files = Get-ChildItem -Path $UserInput -Recurse | Where-Object -FilterScript { !$_.PSIsContainer }
    $hashes = $files | ForEach-Object -Process {Get-FileHash -Path $_.FullName -Algorithm MD5}
    
    $duplicates = $hashes | Group-Object -Property Hash | Where-Object -FilterScript {$_.Count -gt 1}
    
    foreach($duplicate in $duplicates)
    {
        Write-Host -Object "These files share hash : $($duplicate.Group.Path -join ', ')"
    
        # delete first duplicate
        # Remove-Item -Path $duplicate.Group[0].Path -Force -WhatIf
    
        # delete second duplicate
        # Remove-Item -Path $duplicate.Group[1].Path -Force -WhatIf
    
        # delete all duplicates except the first
        # foreach($duplicatePath in ($duplicate.Group.Path | Select-Object -Skip 1))
        # {
        #     Remove-Item -Path $duplicatePath -Force -WhatIf
        # }
    }
    

    取消注释最后的代码以根据您的偏好删除重复项,当您准备好删除文件时,请确保您还删除了 -WhatIf 参数。

    如果我取消注释“删除除第一个以外的所有重复项”,这是我从上述命令收到的输出

    Write a path: 
    H:\
    These files share hash : H:\Rename template 2.csv, H:\Rename template.csv
    What if: Performing the operation "Remove File" on target "H:\Rename template.csv".
    

    【讨论】:

    • 因此您实际上可以删除重复项,因为您将其他哈希和重复项用作“对象”,对吗?就我而言,我递归地进行搜索(所以我得到所有文件,以及目录中的文件等)。我已经尝试过了,它并没有递归,所以它可能只是添加 -recursive 部分,就是这样,对吧?我也尝试过“删除除第一个之外的所有重复项”,它不会删除实际文件!另外,“重复”是一个数组,对吧?
    • 是的,您将需要 Recurse 参数,我在没有它的情况下进行了测试,忘记添加它。运行代码时是否收到任何错误消息?我能够成功运行它(使用-Recurse 参数。
    • 现在它工作得非常好!好吧,它说它已经删除了文件,但是当我去目录查看文件是否已被删除时,会弹出消息说它们已经被删除但没有。
    • 当你想真正删除文件时,确保你已经删除了WhatIf参数,如果你把它留在线上,它会说Performing action "Delete" on "C:\Folder1\Duplicate1.txt",但它实际上不会删除任何东西。
    • 是的!我注意到它很快并删除了它!感谢您的帮助!
    猜你喜欢
    • 2016-10-25
    • 2020-03-26
    • 2016-03-11
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多