remove duplicate files

This commit is contained in:
Nathan
2025-12-19 13:20:58 -07:00
parent 5232e7c2cc
commit 5a7d1f66b9
2 changed files with 369 additions and 36 deletions

View File

@@ -130,7 +130,6 @@ function Process-DuplicateGroup {
param(
[array]$Files,
[string]$CommonPath,
[string]$DuplicatesPath,
[hashtable]$FilesInCommon,
[switch]$StripPrefix,
[string[]]$ValidPrefixes = @()
@@ -237,27 +236,14 @@ function Process-DuplicateGroup {
Write-Warning "Failed to move first duplicate file: $($firstFile.FullName) - $($_.Exception.Message)"
}
# Move remaining files to \common\duplicates with numbered suffixes
# Delete remaining duplicate files (they're replaced by the common file)
for ($i = 1; $i -lt $Files.Count; $i++) {
$fileObj = $Files[$i].File
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileObj.Name)
$extension = [System.IO.Path]::GetExtension($fileObj.Name)
$numberedFileName = "${baseName}.$($i.ToString('000'))${extension}"
$duplicateDestinationPath = Join-Path -Path $DuplicatesPath -ChildPath $numberedFileName
# Handle name conflicts in duplicates folder
$conflictCounter = 1
while (Test-Path -Path $duplicateDestinationPath) {
$numberedFileName = "${baseName}.$($i.ToString('000'))_${conflictCounter}${extension}"
$duplicateDestinationPath = Join-Path -Path $DuplicatesPath -ChildPath $numberedFileName
$conflictCounter++
}
try {
Move-Item -Path $fileObj.FullName -Destination $duplicateDestinationPath -Force
Remove-Item -Path $fileObj.FullName -Force
$duplicateCount++
} catch {
Write-Warning "Failed to move duplicate file: $($fileObj.FullName) - $($_.Exception.Message)"
Write-Warning "Failed to delete duplicate file: $($fileObj.FullName) - $($_.Exception.Message)"
}
}
@@ -307,18 +293,13 @@ if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
Write-Host " Found $($groupedByChecksum.Count) unique checksums." -ForegroundColor Gray
# Create [blendfile]\common and [blendfile]\common\duplicates directories
# Create [blendfile]\common directory
$blendfileCommonPath = Join-Path -Path $blendfileFolder.FullName -ChildPath "common"
$blendfileDuplicatesPath = Join-Path -Path $blendfileCommonPath -ChildPath "duplicates"
if (-not (Test-Path -Path $blendfileCommonPath -PathType Container)) {
New-Item -ItemType Directory -Path $blendfileCommonPath | Out-Null
}
if (-not (Test-Path -Path $blendfileDuplicatesPath -PathType Container)) {
New-Item -ItemType Directory -Path $blendfileDuplicatesPath | Out-Null
}
# Track filenames already in [blendfile]\common
$filesInBlendfileCommon = @{}
@@ -327,18 +308,18 @@ if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
$blendfileDuplicates = 0
foreach ($group in $groupedByChecksum) {
$result = Process-DuplicateGroup -Files $group.Group -CommonPath $blendfileCommonPath -DuplicatesPath $blendfileDuplicatesPath -FilesInCommon $filesInBlendfileCommon
$result = Process-DuplicateGroup -Files $group.Group -CommonPath $blendfileCommonPath -FilesInCommon $filesInBlendfileCommon
$blendfileMoved += $result.MovedCount
$blendfileDuplicates += $result.DuplicateCount
}
Write-Host " Moved $blendfileMoved file(s) to \common, $blendfileDuplicates duplicate(s) to \common\duplicates" -ForegroundColor Green
Write-Host " Moved $blendfileMoved file(s) to \common, deleted $blendfileDuplicates duplicate(s)" -ForegroundColor Green
$totalPass1Moved += $blendfileMoved
$totalPass1Duplicates += $blendfileDuplicates
}
Write-Host ""
Write-Host "Pass 1 complete: $totalPass1Moved file(s) moved, $totalPass1Duplicates duplicate(s) moved" -ForegroundColor Green
Write-Host "Pass 1 complete: $totalPass1Moved file(s) moved, $totalPass1Duplicates duplicate(s) deleted" -ForegroundColor Green
}
# ============================================================================
@@ -376,25 +357,19 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
Write-Host "Found $($groupedByChecksum.Count) unique checksums." -ForegroundColor Green
# Create \textures\common and \textures\common\duplicates directories
# Create \textures\common directory
$rootCommonPath = Join-Path -Path $textureFolderPath -ChildPath "common"
$rootDuplicatesPath = Join-Path -Path $rootCommonPath -ChildPath "duplicates"
if (-not (Test-Path -Path $rootCommonPath -PathType Container)) {
New-Item -ItemType Directory -Path $rootCommonPath | Out-Null
Write-Host "Created directory: $rootCommonPath" -ForegroundColor Green
}
if (-not (Test-Path -Path $rootDuplicatesPath -PathType Container)) {
New-Item -ItemType Directory -Path $rootDuplicatesPath | Out-Null
Write-Host "Created directory: $rootDuplicatesPath" -ForegroundColor Green
}
# Track filenames already in \textures\common
$filesInRootCommon = @{}
# Process each checksum group
Write-Host "Moving files to \common and \common\duplicates..." -ForegroundColor Yellow
Write-Host "Moving files to \common and deleting duplicates..." -ForegroundColor Yellow
$pass2Moved = 0
$pass2Duplicates = 0
@@ -408,13 +383,13 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
# Multiple files with same checksum (duplicates across blendfiles)
# Use StripPrefix to remove blendfile prefixes (e.g., "Demarco_", "Chan_") when all files share the same suffix
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -DuplicatesPath $rootDuplicatesPath -FilesInCommon $filesInRootCommon -StripPrefix -ValidPrefixes $validBlendfilePrefixes
$result = Process-DuplicateGroup -Files $files -CommonPath $rootCommonPath -FilesInCommon $filesInRootCommon -StripPrefix -ValidPrefixes $validBlendfilePrefixes
$pass2Moved += $result.MovedCount
$pass2Duplicates += $result.DuplicateCount
}
Write-Host ""
Write-Host "Pass 2 complete: $pass2Moved file(s) moved to \common, $pass2Duplicates duplicate(s) moved to \common\duplicates" -ForegroundColor Green
Write-Host "Pass 2 complete: $pass2Moved file(s) moved to \common, $pass2Duplicates duplicate(s) deleted" -ForegroundColor Green
}
Write-Host ""