Removing duplicate files with Powershell

后端 未结 4 2055
渐次进展
渐次进展 2021-02-14 21:39

I have several thousand duplicate files (jar files as an example) that I\'d like to use powershell to

  1. Search through the file system recursively
  2. Find the
4条回答
  •  名媛妹妹
    2021-02-14 22:04

    Keep a dictionary of files, delete when the next file name was already encountered before:

    $dict = @{};
    dir c:\admin -Recurse | foreach {
      $key = $_.Name #replace this with your checksum function
      $find = $dict[$key];
      if($find -ne $null) {
        #current file is a duplicate
        #Remove-Item -Path $_.FullName ?    
      }
      $dict[$key] = 0; #dummy placeholder to save memory
    }
    

    I used file name as a key, but you can use a checksum if you want (or both) - see code comment.

提交回复
热议问题