Files
ProjectStructure/organize_textures.ps1
2025-12-19 13:20:58 -07:00

397 lines
15 KiB
PowerShell

# Script to organize texture files by checksum with two-level duplicate detection
# Pass 1: Intra-blendfile duplicates → [blendfile]\common
# Pass 2: Inter-blendfile duplicates → \textures\common
# Usage: .\organize_textures.ps1
# Prompt user for texture folder path
$textureFolderPath = Read-Host "Enter texture folder path"
# Validate the input path
if ([string]::IsNullOrWhiteSpace($textureFolderPath)) {
Write-Host "Error: No path provided." -ForegroundColor Red
exit
}
if (-not (Test-Path -Path $textureFolderPath -PathType Container)) {
Write-Host "Error: Path does not exist or is not a directory: $textureFolderPath" -ForegroundColor Red
exit
}
# Resolve the full path
$textureFolderPath = (Resolve-Path $textureFolderPath).ProviderPath
Write-Host "Processing texture folder: $textureFolderPath" -ForegroundColor Cyan
# Function to calculate checksums for files
function Get-FilesWithChecksums {
param(
[array]$Files
)
$throttleLimit = [Math]::Max(1, [Environment]::ProcessorCount)
$parallelScriptBlock = {
try {
$hash = Get-FileHash -Path $_.FullName -Algorithm SHA256
[PSCustomObject]@{
File = $_
Hash = $hash.Hash
}
} catch {
Write-Warning "Failed to calculate checksum for: $($_.FullName) - $($_.Exception.Message)"
$null
}
}
return $Files | ForEach-Object -Parallel $parallelScriptBlock -ThrottleLimit $throttleLimit | Where-Object { $null -ne $_ }
}
# Function to move files to common folder
function Move-FilesToCommon {
param(
[array]$Files,
[string]$CommonPath,
[string]$DuplicatesPath,
[hashtable]$FilesInCommon
)
$movedCount = 0
$duplicateCount = 0
foreach ($fileObj in $Files) {
$fileName = $fileObj.Name
$destinationPath = Join-Path -Path $CommonPath -ChildPath $fileName
# Handle name conflicts
if ($FilesInCommon.ContainsKey($fileName)) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
$extension = [System.IO.Path]::GetExtension($fileName)
$counter = 1
do {
$newFileName = "${baseName}_${counter}${extension}"
$destinationPath = Join-Path -Path $CommonPath -ChildPath $newFileName
$counter++
} while ($FilesInCommon.ContainsKey($newFileName) -or (Test-Path -Path $destinationPath))
$fileName = $newFileName
}
try {
Move-Item -Path $fileObj.FullName -Destination $destinationPath -Force
$FilesInCommon[$fileName] = $true
$movedCount++
} catch {
Write-Warning "Failed to move file: $($fileObj.FullName) - $($_.Exception.Message)"
}
}
return @{
MovedCount = $movedCount
DuplicateCount = $duplicateCount
}
}
# Function to extract suffix after blendfile prefix (e.g., "Demarco_Std_Teeth_ao.jpg" -> "Std_Teeth_ao.jpg")
# Only strips prefixes that are in the $ValidPrefixes list
function Get-FileNameWithoutPrefix {
param(
[string]$FileName,
[string[]]$ValidPrefixes
)
# Validate input
if ([string]::IsNullOrWhiteSpace($FileName)) {
return $FileName
}
# If no valid prefixes provided, return original filename
if ($null -eq $ValidPrefixes -or $ValidPrefixes.Count -eq 0) {
return $FileName
}
# Use Split instead of regex for robustness (avoids $matches variable issues in loops)
# Split by the first underscore only (limit to 2 parts)
$parts = $FileName.Split('_', 2)
if ($parts.Length -eq 2) {
$prefix = $parts[0]
$suffix = $parts[1]
# Only strip if the prefix is in the valid prefixes list
if ($ValidPrefixes -contains $prefix -and -not [string]::IsNullOrWhiteSpace($suffix)) {
return $suffix
}
}
# No valid prefix found or suffix is empty, return original filename
return $FileName
}
# Function to process duplicate group
function Process-DuplicateGroup {
param(
[array]$Files,
[string]$CommonPath,
[hashtable]$FilesInCommon,
[switch]$StripPrefix,
[string[]]$ValidPrefixes = @()
)
$movedCount = 0
$duplicateCount = 0
if ($Files.Count -eq 1) {
# Single file - leave in place (will be processed in Pass 2)
return @{
MovedCount = 0
DuplicateCount = 0
}
}
# Validate files array
if ($null -eq $Files -or $Files.Count -eq 0) {
return @{
MovedCount = 0
DuplicateCount = 0
}
}
# Multiple files with same checksum (duplicates)
# Determine the filename to use
$firstFile = $Files[0].File
if ($null -eq $firstFile -or [string]::IsNullOrWhiteSpace($firstFile.Name)) {
Write-Warning "Invalid file object in duplicate group"
return @{
MovedCount = 0
DuplicateCount = 0
}
}
$fileName = $firstFile.Name
# If StripPrefix is enabled, always strip prefix from the filename
if ($StripPrefix) {
Write-Host " [DEBUG] StripPrefix enabled for group with $($Files.Count) file(s)" -ForegroundColor Magenta
# Always try to strip prefix - if file has a prefix, it will be removed
$strippedName = Get-FileNameWithoutPrefix -FileName $firstFile.Name -ValidPrefixes $ValidPrefixes
Write-Host " [DEBUG] Original: '$($firstFile.Name)' -> Stripped: '$strippedName'" -ForegroundColor Gray
# Use stripped name if it's different from original (prefix was removed)
# Otherwise keep original (file had no prefix to strip)
if ($strippedName -ne $firstFile.Name) {
$fileName = $strippedName
Write-Host " [DEBUG] Using stripped name: '$fileName'" -ForegroundColor Green
} else {
Write-Host " [DEBUG] No prefix found, keeping original: '$fileName'" -ForegroundColor Yellow
}
# If we have multiple files with same checksum, try to find common suffix
# This handles cases where files from different blendfiles share the same texture
if ($Files.Count -gt 1) {
$allSuffixes = @()
foreach ($fileObj in $Files) {
# $fileObj is a PSCustomObject with .File property, so access .File.Name
$suffix = Get-FileNameWithoutPrefix -FileName $fileObj.File.Name -ValidPrefixes $ValidPrefixes
$allSuffixes += $suffix
}
Write-Host " [DEBUG] All suffixes: $($allSuffixes -join ', ')" -ForegroundColor Gray
# If all files strip to the same suffix, use that
$uniqueSuffixes = @($allSuffixes | Select-Object -Unique)
if ($uniqueSuffixes.Count -eq 1 -and $uniqueSuffixes[0] -ne $firstFile.Name) {
$fileName = [string]$uniqueSuffixes[0]
Write-Host " [DEBUG] All files share common suffix, using: '$fileName'" -ForegroundColor Green
}
}
Write-Host " [DEBUG] Final filename: '$fileName'" -ForegroundColor Cyan
}
# Ensure fileName is never null or empty
if ([string]::IsNullOrWhiteSpace($fileName)) {
$fileName = $firstFile.Name
}
$destinationPath = Join-Path -Path $CommonPath -ChildPath $fileName
# Handle name conflicts for the first file
if ($FilesInCommon.ContainsKey($fileName)) {
$baseName = [System.IO.Path]::GetFileNameWithoutExtension($fileName)
$extension = [System.IO.Path]::GetExtension($fileName)
$counter = 1
do {
$newFileName = "${baseName}_${counter}${extension}"
$destinationPath = Join-Path -Path $CommonPath -ChildPath $newFileName
$counter++
} while ($FilesInCommon.ContainsKey($newFileName) -or (Test-Path -Path $destinationPath))
$fileName = $newFileName
}
try {
Move-Item -Path $firstFile.FullName -Destination $destinationPath -Force
$FilesInCommon[$fileName] = $true
$movedCount++
} catch {
Write-Warning "Failed to move first duplicate file: $($firstFile.FullName) - $($_.Exception.Message)"
}
# Delete remaining duplicate files (they're replaced by the common file)
for ($i = 1; $i -lt $Files.Count; $i++) {
$fileObj = $Files[$i].File
try {
Remove-Item -Path $fileObj.FullName -Force
$duplicateCount++
} catch {
Write-Warning "Failed to delete duplicate file: $($fileObj.FullName) - $($_.Exception.Message)"
}
}
return @{
MovedCount = $movedCount
DuplicateCount = $duplicateCount
}
}
# ============================================================================
# PASS 1: Intra-Blendfile Processing
# ============================================================================
Write-Host ""
Write-Host "=== PASS 1: Intra-Blendfile Processing ===" -ForegroundColor Cyan
# Get all direct subdirectories of texture folder (blendfile folders)
$blendfileFolders = Get-ChildItem -Path $textureFolderPath -Directory | Where-Object { $_.Name -ne "common" }
if ($null -eq $blendfileFolders -or $blendfileFolders.Count -eq 0) {
Write-Host "No blendfile folders found. Skipping Pass 1." -ForegroundColor Yellow
} else {
Write-Host "Found $($blendfileFolders.Count) blendfile folder(s) to process." -ForegroundColor Green
$totalPass1Moved = 0
$totalPass1Duplicates = 0
foreach ($blendfileFolder in $blendfileFolders) {
Write-Host ""
Write-Host "Processing blendfile: $($blendfileFolder.Name)" -ForegroundColor Yellow
# Get all files in this blendfile folder, excluding \common folders
$blendfileFiles = Get-ChildItem -Path $blendfileFolder.FullName -Recurse -File | Where-Object { $_.FullName -notlike "*\common\*" }
if ($null -eq $blendfileFiles -or $blendfileFiles.Count -eq 0) {
Write-Host " No files found in this blendfile folder." -ForegroundColor Gray
continue
}
Write-Host " Found $($blendfileFiles.Count) files." -ForegroundColor Gray
# Calculate checksums
Write-Host " Calculating checksums..." -ForegroundColor Gray
$filesWithChecksums = Get-FilesWithChecksums -Files $blendfileFiles
# Group by checksum
$groupedByChecksum = $filesWithChecksums | Group-Object -Property Hash
Write-Host " Found $($groupedByChecksum.Count) unique checksums." -ForegroundColor Gray
# Create [blendfile]\common directory
$blendfileCommonPath = Join-Path -Path $blendfileFolder.FullName -ChildPath "common"
if (-not (Test-Path -Path $blendfileCommonPath -PathType Container)) {
New-Item -ItemType Directory -Path $blendfileCommonPath | Out-Null
}
# Track filenames already in [blendfile]\common
$filesInBlendfileCommon = @{}
# Process each checksum group
$blendfileMoved = 0
$blendfileDuplicates = 0
foreach ($group in $groupedByChecksum) {
$result = Process-DuplicateGroup -Files $group.Group -CommonPath $blendfileCommonPath -FilesInCommon $filesInBlendfileCommon
$blendfileMoved += $result.MovedCount
$blendfileDuplicates += $result.DuplicateCount
}
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) deleted" -ForegroundColor Green
}
# ============================================================================
# PASS 2: Inter-Blendfile Processing
# ============================================================================
Write-Host ""
Write-Host "=== PASS 2: Inter-Blendfile Processing ===" -ForegroundColor Cyan
# Build list of valid blendfile prefixes from folder names
$validBlendfilePrefixes = @()
$blendfileFoldersForPrefixes = Get-ChildItem -Path $textureFolderPath -Directory | Where-Object { $_.Name -ne "common" }
if ($null -ne $blendfileFoldersForPrefixes) {
$validBlendfilePrefixes = $blendfileFoldersForPrefixes | ForEach-Object { $_.Name }
Write-Host "Valid blendfile prefixes: $($validBlendfilePrefixes -join ', ')" -ForegroundColor Gray
}
# Get all remaining files (excluding all \common folders)
Write-Host "Collecting remaining files..." -ForegroundColor Yellow
$remainingFiles = Get-ChildItem -Path $textureFolderPath -Recurse -File | Where-Object { $_.FullName -notlike "*\common\*" }
if ($null -eq $remainingFiles -or $remainingFiles.Count -eq 0) {
Write-Host "No remaining files found to process." -ForegroundColor Yellow
} else {
Write-Host "Found $($remainingFiles.Count) remaining files to process." -ForegroundColor Green
# Calculate checksums
Write-Host "Calculating checksums using parallel processing (this may take a while)..." -ForegroundColor Yellow
$filesWithChecksums = Get-FilesWithChecksums -Files $remainingFiles
Write-Host "Checksum calculation complete." -ForegroundColor Green
# Group files by checksum
Write-Host "Grouping files by checksum..." -ForegroundColor Yellow
$groupedByChecksum = $filesWithChecksums | Group-Object -Property Hash
Write-Host "Found $($groupedByChecksum.Count) unique checksums." -ForegroundColor Green
# Create \textures\common directory
$rootCommonPath = Join-Path -Path $textureFolderPath -ChildPath "common"
if (-not (Test-Path -Path $rootCommonPath -PathType Container)) {
New-Item -ItemType Directory -Path $rootCommonPath | Out-Null
Write-Host "Created directory: $rootCommonPath" -ForegroundColor Green
}
# Track filenames already in \textures\common
$filesInRootCommon = @{}
# Process each checksum group
Write-Host "Moving files to \common and deleting duplicates..." -ForegroundColor Yellow
$pass2Moved = 0
$pass2Duplicates = 0
foreach ($group in $groupedByChecksum) {
$files = $group.Group
if ($files.Count -eq 1) {
# Single file - leave in place (unique file)
continue
}
# 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 -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) deleted" -ForegroundColor Green
}
Write-Host ""
Write-Host "File organization complete!" -ForegroundColor Green