Last active
June 15, 2023 16:49
-
-
Save mavaddat/fb50d6fb1173345a55aea582390f8585 to your computer and use it in GitHub Desktop.
Resolve OneDrive conflicts in automatically renamed files
This file contains hidden or 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
$winMergeExe = "$env:LOCALAPPDATA\Programs\WinMerge\WinMergeU.exe" | |
$oneDrivePaths = Get-Item -Path Env:\OneDrive* | Select-Object -Unique -ExpandProperty Value | |
# $duplicatePattern = New-Object -TypeName regex -ArgumentList @('(.+)(?:-[^-\s]+$|\.[^\.]+\.bak$)', [System.Text.RegularExpressions.RegexOptions]::Compiled,[timespan]::FromMilliseconds(100)) | |
$duplicatePattern = New-Object -TypeName regex -ArgumentList @("(.+)(?:-${env:COMPUTERNAME}(?:-\d+)*`$)", [System.Text.RegularExpressions.RegexOptions]::Compiled,[timespan]::FromMilliseconds(100)) | |
foreach($onedrive in $oneDrivePaths){ | |
Get-ChildItem -Path $onedrive -File -Recurse | Where-Object -FilterScript { $_.BaseName -match $duplicatePattern -and (Test-Path -Path (Join-Path -Path ($_.Directory.FullName) -ChildPath ($Matches[1] + $_.Extension)) -PathType Leaf)} | ForEach-Object { | |
$original = Join-Path -Path ($_.Directory.FullName) -ChildPath ($Matches[1] + $_.Extension) | |
$duplicate = $_.FullName | |
$originalHash = Get-FileHash -Path $original | |
$duplicateHash = Get-FileHash -Path $duplicate | |
if($originalHash.Hash -eq $duplicateHash.Hash){ | |
Remove-Item -Path $duplicate | |
} else { | |
&$winMergeExe $original $duplicate | |
Remove-Item -Path $duplicate -Confirm | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment