Last active
June 22, 2021 21:04
-
-
Save asmitde/493da78c3e88b45e31d0ec6f526ec5ee to your computer and use it in GitHub Desktop.
Script for removing duplicate files from OneDrive Documents folder [For Abhishek]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RemoveDuplicates | |
{ | |
[string]$backup_dir | |
[string]$suffix | |
RemoveDuplicates( | |
[string]$backupFolder, | |
[string]$suffixString | |
) { | |
$this.backup_dir = $backupFolder | |
$this.suffix = $suffixString | |
if (-not (Test-Path -Path $this.backup_dir)) { | |
Write-Host "❕ [INFO] Creating a backup folder in $($this.backup_dir)" | |
New-Item $this.backup_dir -ItemType Directory -Verbose | |
} | |
else { | |
Write-Host "⚠️ [INFO] Backup folder $($this.backup_dir) already exists" -ForegroundColor Yellow | |
} | |
} | |
[void] StartProcess([string] $folderName) | |
{ | |
$entities = Get-ChildItem -Path $folderName -ErrorAction SilentlyContinue -Force | |
$fileset = New-Object 'System.Collections.Generic.HashSet[string]' | |
$non_copy_files = $entities | Where-Object { ($_ -is [System.IO.FileInfo]) -and (-not $_.BaseName.EndsWith($this.suffix)) } | |
foreach ($file in $non_copy_files) { | |
$fileset.Add($file.FullName) | |
} | |
foreach ($entry in $entities) { | |
if ($entry -is [System.IO.DirectoryInfo]) { | |
$this.StartProcess($entry.FullName) | |
} | |
else { # [System.IO.FileInfo] | |
if ($entry.BaseName.EndsWith($this.suffix)) { | |
Write-Host "❕ [INFO] Found file: $($entry.FullName)" | |
$non_copy_name = $entry.DirectoryName+"\"+$entry.BaseName.Replace($this.suffix, "")+$entry.Extension | |
if ($fileset.Contains($non_copy_name)) { # a corresponding non_copy exists in hashset | |
# Assert that the non_copy file exists | |
if (-not (Test-Path -Path $non_copy_name)) { | |
Write-Host "❌ [ERROR] File $non_copy_name not found. Aborting." -ForegroundColor Red # This should not trigger! | |
Exit | |
} | |
Write-Host "❕ [INFO] Found duplicate file: $non_copy_name" | |
# Backup the older non_copy file | |
$non_copy_file = Get-Item -Path $non_copy_name | |
$non_copy_name_with_ts = $non_copy_file.BaseName+"_"+$non_copy_file.GetHashCode()+$non_copy_file.Extension | |
Move-Item -Path $non_copy_name -Destination "$($this.backup_dir)\$non_copy_name_with_ts" -Verbose | |
# Rename the newer _copy file | |
Rename-Item -Path $entry.FullName -NewName $non_copy_name -Verbose | |
} | |
else { | |
Write-Host "⚠️ [INFO] No corresponding duplicate file." -ForegroundColor Yellow | |
} | |
} | |
} | |
} | |
} | |
} | |
Write-Host "==== Triaging OneDrive Documents folder ====" -ForegroundColor DarkMagenta | |
$desktop = [System.Environment]::GetFolderPath("Desktop") | |
$documents = [System.Environment]::GetFolderPath("MyDocuments") | |
Write-Host "Found OneDrive Documents folder in $documents" | |
$in = Read-host "Is this correct❔ (y/n)" | |
if ($in.ToLower().StartsWith('n')) | |
{ | |
Write-Host "❌ [ERROR] Oops, unable to proceed. Contact Asmit." -ForegroundColor Red | |
Exit | |
} | |
$backup_folder_name = "duplicate_files" | |
$backup_path = Join-Path -Path $desktop -ChildPath $backup_folder_name | |
[RemoveDuplicates]$rmdup = [RemoveDuplicates]::new($backup_path, "_copy") | |
$rmdup.StartProcess($documents) | |
Write-Host "✔️ [DONE] Triaging files completed. Check OneDrive Documents folder. Older files are available in $backup_path" -ForegroundColor Green | |
Write-Host "👉 [TODO] Give Asmit a treat 😎" -ForegroundColor DarkYellow |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment