Advertisement
Guest User

Untitled

a guest
Jun 1st, 2024
505
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | Source Code | 0 0
  1. <#
  2. .SYNOPSIS
  3. Identifie duplicate files in a comparison folder based on the contents of a source folder.
  4. .DESCRIPTION
  5. This script calculates the hash values of files in a source folder and compares them against the files in a comparison folder.
  6. If duplicates are found, it optionally renames the duplicate files in the comparison folder by prepending "Duplicate-" to their filenames.
  7. .PARAMETER SourceFolder
  8. The path to the source folder containing the original files.
  9. .PARAMETER ComparisonFolder
  10. The path to the comparison folder where duplicate files will be identified.
  11. .PARAMETER RenameDuplicates
  12. An optional switch parameter. If specified, the script renames duplicate files in the comparison folder by prepending "Duplicate-" to their filenames.
  13. .EXAMPLE
  14. .\Find-DuplicateFiles.ps1 -SourceFolder "C:\path\to\source\folder" -ComparisonFolder "C:\path\to\compare\folder"
  15. This command will identify duplicate files in the "C:\path\to\backup" folder based on the files in the "C:\path\to\main" folder.
  16. .EXAMPLE
  17. .\Find-DuplicateFiles.ps1 -SourceFolder "C:\path\to\source\folder" -ComparisonFolder "C:\path\to\compare\folder" -RenameDuplicates
  18. This command will identify and rename duplicate files in the "C:\path\to\compare\folder" folder by prepending "Duplicate-" to their filenames based on the files in the "C:\path\to\main" folder.
  19. .NOTES
  20. Date: 2024-05-26
  21. #>
  22. param (
  23. [Parameter(Mandatory = $true)]
  24. [string]$SourceFolder, # Main folder
  25.  
  26. [Parameter(Mandatory = $true)]
  27. [string]$ComparisonFolder, # Backup folder
  28.  
  29. [Parameter(Mandatory = $false)]
  30. [switch]$RenameDuplicates # Optional switch to rename duplicates
  31. )
  32.  
  33. # Function to calculate the hash of a file
  34. function Get-CustomFileHash {
  35. param (
  36. [string]$filePath
  37. )
  38. $hash = Get-FileHash -Algorithm MD5 -Path $filePath
  39. return $hash.Hash
  40. }
  41.  
  42. $sourceFiles = Get-ChildItem -Path $SourceFolder -File
  43. $comparisonFiles = Get-ChildItem -Path $ComparisonFolder -File
  44. $sourceFileHashes = @{}
  45.  
  46. foreach ($file in $sourceFiles) {
  47. $hash = Get-CustomFileHash -filePath $file.FullName
  48. $sourceFileHashes[$hash] = $file.FullName
  49. }
  50.  
  51. $duplicateFiles = @()
  52. $index = 1
  53. foreach ($file in $comparisonFiles) {
  54. $hash = Get-CustomFileHash -filePath $file.FullName
  55. if ($sourceFileHashes.ContainsKey($hash)) {
  56. $newFileName = $null
  57. if ($RenameDuplicates) {
  58. $newFileName = Join-Path -Path $ComparisonFolder -ChildPath ("Duplicate-" + $file.Name)
  59. Rename-Item -Path $file.FullName -NewName $newFileName
  60. }
  61.  
  62. $duplicateFiles += [PSCustomObject]@{
  63. Number = $index
  64. ComparisonFile = $file.FullName
  65. SourceFile = $sourceFileHashes[$hash]
  66. RenamedTo = $newFileName
  67. }
  68. $index++
  69. }
  70. }
  71.  
  72. $duplicateFiles | Format-Table -AutoSize
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement