reorganize FlatColors and remove empty folders

This commit is contained in:
Nathan
2025-12-19 15:34:07 -07:00
parent 5a7d1f66b9
commit 7896059a86
2 changed files with 1024 additions and 5 deletions

View File

@@ -336,7 +336,15 @@ if ($null -ne $blendfileFoldersForPrefixes) {
Write-Host "Valid blendfile prefixes: $($validBlendfilePrefixes -join ', ')" -ForegroundColor Gray
}
# Get all remaining files (excluding all \common folders)
# Collect FlatColors files separately (including those in \common\FlatColors folders, but not from root \common\FlatColors)
Write-Host "Collecting FlatColors files..." -ForegroundColor Yellow
$rootCommonFlatColorsPath = Join-Path -Path $textureFolderPath -ChildPath "common\FlatColors"
$allFlatColorsFiles = Get-ChildItem -Path $textureFolderPath -Recurse -File | Where-Object {
($_.FullName -like "*\FlatColors\*" -or $_.Name -like "*FlatColors*") -and
$_.FullName -notlike "$([regex]::Escape($rootCommonFlatColorsPath))\*"
}
# Get all remaining files (excluding all \common folders, but we'll handle FlatColors separately)
Write-Host "Collecting remaining files..." -ForegroundColor Yellow
$remainingFiles = Get-ChildItem -Path $textureFolderPath -Recurse -File | Where-Object { $_.FullName -notlike "*\common\*" }
@@ -351,9 +359,17 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
Write-Host "Checksum calculation complete." -ForegroundColor Green
# Group files by checksum
# Separate FlatColors files from other files before processing
Write-Host "Identifying FlatColors files..." -ForegroundColor Yellow
$flatColorsFiles = $allFlatColorsFiles | Where-Object { (Test-Path -Path $_.FullName) }
$nonFlatColorsFiles = $remainingFiles | Where-Object { $_.Name -notlike "*FlatColors*" }
Write-Host "Found $($flatColorsFiles.Count) FlatColors file(s) (including from \common\FlatColors folders), $($nonFlatColorsFiles.Count) other file(s)" -ForegroundColor Gray
# Group non-FlatColors files by checksum
Write-Host "Grouping files by checksum..." -ForegroundColor Yellow
$groupedByChecksum = $filesWithChecksums | Group-Object -Property Hash
$nonFlatColorsWithChecksums = $filesWithChecksums | Where-Object { $_.File.Name -notlike "*FlatColors*" }
$groupedByChecksum = $nonFlatColorsWithChecksums | Group-Object -Property Hash
Write-Host "Found $($groupedByChecksum.Count) unique checksums." -ForegroundColor Green
@@ -368,7 +384,7 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
# Track filenames already in \textures\common
$filesInRootCommon = @{}
# Process each checksum group
# Process each checksum group (excluding FlatColors)
Write-Host "Moving files to \common and deleting duplicates..." -ForegroundColor Yellow
$pass2Moved = 0
$pass2Duplicates = 0
@@ -388,9 +404,132 @@ if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
$pass2Duplicates += $result.DuplicateCount
}
# Process FlatColors files: merge duplicates and move to \common\FlatColors
Write-Host "Processing FlatColors files (merging duplicates)..." -ForegroundColor Yellow
if ($null -ne $flatColorsFiles -and $flatColorsFiles.Count -gt 0) {
Write-Host "Found $($flatColorsFiles.Count) FlatColors file(s) to process" -ForegroundColor Gray
# Calculate checksums for FlatColors files
Write-Host "Calculating checksums for FlatColors files..." -ForegroundColor Yellow
$flatColorsWithChecksums = Get-FilesWithChecksums -Files $flatColorsFiles
# Group by checksum to find duplicates
$flatColorsGroupedByChecksum = $flatColorsWithChecksums | Group-Object -Property Hash
Write-Host "Found $($flatColorsGroupedByChecksum.Count) unique FlatColors file(s)" -ForegroundColor Gray
# Create \textures\common\FlatColors directory
$flatColorsPath = Join-Path -Path $rootCommonPath -ChildPath "FlatColors"
if (-not (Test-Path -Path $flatColorsPath -PathType Container)) {
New-Item -ItemType Directory -Path $flatColorsPath | Out-Null
Write-Host "Created directory: $flatColorsPath" -ForegroundColor Green
}
# Track filenames already in \common\FlatColors
$filesInFlatColors = @{}
$flatColorsMoved = 0
$flatColorsDuplicates = 0
foreach ($group in $flatColorsGroupedByChecksum) {
$files = $group.Group
if ($files.Count -eq 1) {
# Single file - move to FlatColors folder
$fileObj = $files[0].File
$fileName = $fileObj.Name
# Strip blendfile prefix if present
$strippedName = Get-FileNameWithoutPrefix -FileName $fileName -ValidPrefixes $validBlendfilePrefixes
if ($strippedName -ne $fileName) {
$fileName = $strippedName
}
$destinationPath = Join-Path -Path $flatColorsPath -ChildPath $fileName
# Handle name conflicts (shouldn't happen for unique files, but just in case)
if ($filesInFlatColors.ContainsKey($fileName) -or (Test-Path -Path $destinationPath)) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
$extension = [System.IO.Path]::GetExtension($fileName)
$counter = 1
do {
$newFileName = "${baseName}_${counter}${extension}"
$destinationPath = Join-Path -Path $flatColorsPath -ChildPath $newFileName
$counter++
} while ($filesInFlatColors.ContainsKey($newFileName) -or (Test-Path -Path $destinationPath))
$fileName = $newFileName
}
try {
Move-Item -Path $fileObj.FullName -Destination $destinationPath -Force
$filesInFlatColors[$fileName] = $true
$flatColorsMoved++
} catch {
Write-Warning "Failed to move FlatColors file: $($fileObj.FullName) - $($_.Exception.Message)"
}
} else {
# Multiple files with same checksum (duplicates) - merge them
# Use Process-DuplicateGroup to handle merging
$result = Process-DuplicateGroup -Files $files -CommonPath $flatColorsPath -FilesInCommon $filesInFlatColors -StripPrefix -ValidPrefixes $validBlendfilePrefixes
$flatColorsMoved += $result.MovedCount
$flatColorsDuplicates += $result.DuplicateCount
}
}
Write-Host "Moved $flatColorsMoved unique FlatColors file(s) to \common\FlatColors, deleted $flatColorsDuplicates duplicate(s)" -ForegroundColor Green
} else {
Write-Host "No FlatColors files found to process." -ForegroundColor Gray
}
Write-Host ""
Write-Host "Pass 2 complete: $pass2Moved file(s) moved to \common, $pass2Duplicates duplicate(s) deleted" -ForegroundColor Green
}
# Function to remove empty folders recursively
function Remove-EmptyFolders {
param(
[string]$Path,
[string]$RootPath
)
$removedCount = 0
# Get all subdirectories
$subdirs = Get-ChildItem -Path $Path -Directory -ErrorAction SilentlyContinue
if ($null -ne $subdirs) {
foreach ($subdir in $subdirs) {
# Recursively process subdirectories first
$removedCount += Remove-EmptyFolders -Path $subdir.FullName -RootPath $RootPath
# Check if this directory is now empty (after processing subdirectories)
$items = Get-ChildItem -Path $subdir.FullName -ErrorAction SilentlyContinue
if ($null -eq $items -or $items.Count -eq 0) {
# Don't remove the root path
if ($subdir.FullName -ne $RootPath) {
try {
Remove-Item -Path $subdir.FullName -Force -ErrorAction Stop
$removedCount++
} catch {
Write-Warning "Failed to remove empty folder: $($subdir.FullName) - $($_.Exception.Message)"
}
}
}
}
}
return $removedCount
}
# Remove empty folders
Write-Host ""
Write-Host "Removing empty folders..." -ForegroundColor Yellow
$emptyFoldersRemoved = Remove-EmptyFolders -Path $textureFolderPath -RootPath $textureFolderPath
if ($emptyFoldersRemoved -gt 0) {
Write-Host "Removed $emptyFoldersRemoved empty folder(s)" -ForegroundColor Green
} else {
Write-Host "No empty folders found to remove." -ForegroundColor Gray
}
Write-Host ""
Write-Host "File organization complete!" -ForegroundColor Green