Files
ProjectStructure_HOME/UpdateSequences.ps1

220 lines
8.5 KiB
PowerShell
Raw Normal View History

[CmdletBinding()]
param(
[switch]$DebugMode
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Sync-SequenceFilenames {
param(
[Parameter(Mandatory)] [string]$SequenceFolderPath,
[Parameter(Mandatory)] [string]$SequenceName,
[string]$LogFile,
[string[]]$Extensions = @('.png','.jpg','.jpeg','.exr','.tif','.tiff','.bmp','.tga')
)
$renamed = 0
$collisions = 0
$errors = 0
$checked = 0
2025-10-23 10:33:23 -06:00
$minFrame = [int]::MaxValue
$maxFrame = -1
$frameCount = 0
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] RENAME CHECK in '$SequenceFolderPath' (seq='$SequenceName')" | Add-Content -LiteralPath $LogFile }
$files = Get-ChildItem -LiteralPath $SequenceFolderPath -File -Recurse -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -notlike '*\_archive\*' -and ($Extensions -contains $_.Extension.ToLower()) }
foreach ($f in $files) {
$checked++
$base = [System.IO.Path]::GetFileNameWithoutExtension($f.Name)
$ext = $f.Extension
$digits = $null
if ($base -match '_(\d{6})$') {
$digits = $Matches[1]
}
elseif ($base -match '(?<!_)\b(\d{6})$') {
$digits = $Matches[1]
}
elseif ($base -match '(\d{4})$') {
$digits = ('00' + $Matches[1])
}
else {
continue
}
2025-10-23 10:33:23 -06:00
try {
$n = [int]$digits
if ($n -lt $minFrame) { $minFrame = $n }
if ($n -gt $maxFrame) { $maxFrame = $n }
$frameCount++
} catch {}
$targetBase = "$SequenceName" + '_' + $digits
if ($base -eq $targetBase) { continue }
$newName = $targetBase + $ext
$newPath = Join-Path $SequenceFolderPath $newName
try {
if (Test-Path -LiteralPath $newPath) {
$collisions++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] RENAME SKIP collision: '$($f.Name)' -> '$newName'" | Add-Content -LiteralPath $LogFile }
continue
}
Rename-Item -LiteralPath $f.FullName -NewName $newName -ErrorAction Stop
$renamed++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] RENAME: '$($f.Name)' -> '$newName'" | Add-Content -LiteralPath $LogFile }
}
catch {
$errors++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] RENAME ERROR for '$($f.Name)': $($_.Exception.Message)" | Add-Content -LiteralPath $LogFile }
}
}
2025-10-23 10:33:23 -06:00
$minOut = $null
$maxOut = $null
if ($frameCount -gt 0) {
$minOut = $minFrame
$maxOut = $maxFrame
}
return [pscustomobject]@{
Renamed = $renamed
Collisions = $collisions
Errors = $errors
Checked = $checked
MinFrame = $minOut
MaxFrame = $maxOut
FrameCount = $frameCount
}
}
function Rename-SequencePreviewMp4 {
param(
[Parameter(Mandatory)] [string]$SequenceFolderPath,
[Parameter(Mandatory)] [string]$SequenceName,
[Parameter(Mandatory)] [int]$StartFrame,
[Parameter(Mandatory)] [int]$EndFrame,
[string]$LogFile
)
$renamed = 0
$collisions = 0
$errors = 0
$checked = 0
$targetName = "$SequenceName-$StartFrame-$EndFrame.mp4"
$targetPath = Join-Path $SequenceFolderPath $targetName
$mp4s = Get-ChildItem -LiteralPath $SequenceFolderPath -File -Filter '*.mp4' -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -notlike '*\_archive\*' }
foreach ($m in $mp4s) {
$checked++
if ($m.Name -eq $targetName) { continue }
try {
if (Test-Path -LiteralPath $targetPath) {
$collisions++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] MP4 RENAME SKIP collision: '$($m.Name)' -> '$targetName'" | Add-Content -LiteralPath $LogFile }
continue
}
Rename-Item -LiteralPath $m.FullName -NewName $targetName -ErrorAction Stop
$renamed++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] MP4 RENAME: '$($m.Name)' -> '$targetName'" | Add-Content -LiteralPath $LogFile }
break
}
catch {
$errors++
if ($LogFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] MP4 RENAME ERROR for '$($m.Name)': $($_.Exception.Message)" | Add-Content -LiteralPath $LogFile }
}
}
return [pscustomobject]@{
Renamed = $renamed
Collisions = $collisions
Errors = $errors
Checked = $checked
}
}
try {
$root = (Get-Location).ProviderPath
2025-11-06 10:47:19 -07:00
$logFile = $null
if ($DebugMode) {
$logFile = Join-Path $root ("UpdateSequences_{0}.log" -f (Get-Date -Format 'yyyyMMdd_HHmmss'))
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] === UpdateSequences started in '$root' ===" | Out-File -LiteralPath $logFile -Encoding UTF8
}
2025-11-06 10:47:19 -07:00
$sequenceFolders = @()
$dailyDirs = Get-ChildItem -LiteralPath $root -Directory -Filter 'daily_*' -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne '_archive' }
foreach ($d in $dailyDirs) {
$seqDirs = @(Get-ChildItem -LiteralPath $d.FullName -Directory -ErrorAction SilentlyContinue | Where-Object { $_.Name -ne '_archive' })
if ($seqDirs.Count -eq 0) {
$sequenceFolders += $d
} else {
$sequenceFolders += $seqDirs
}
}
2025-11-06 10:47:19 -07:00
$directSeqs = Get-ChildItem -LiteralPath $root -Directory -ErrorAction SilentlyContinue |
Where-Object { $_.Name -ne '_archive' -and $_.Name -notlike 'daily_*' }
$sequenceFolders += $directSeqs
2025-11-06 10:47:19 -07:00
$sequenceFolders = $sequenceFolders | Sort-Object -Property FullName -Unique
2025-11-06 10:47:19 -07:00
if (-not $sequenceFolders) {
Write-Host "No sequence folders found." -ForegroundColor Yellow
if ($logFile) { "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] No sequence folders found." | Add-Content -LiteralPath $logFile }
exit 0
}
$totalSequences = 0
$filesRenamedTotal = 0
$renameCollisions = 0
$renameErrors = 0
2025-10-23 10:33:23 -06:00
$mp4RenamedTotal = 0
$mp4Collisions = 0
$mp4Errors = 0
2025-09-16 22:11:55 -06:00
2025-11-06 10:47:19 -07:00
foreach ($seq in $sequenceFolders) {
$totalSequences++
$renameResult = Sync-SequenceFilenames -SequenceFolderPath $seq.FullName -SequenceName $seq.Name -LogFile $logFile
if ($DebugMode -or $renameResult.Renamed -gt 0 -or $renameResult.Collisions -gt 0 -or $renameResult.Errors -gt 0) {
Write-Host "[RENAME]|$($seq.FullName)|$($seq.Name)|checked=$($renameResult.Checked)|renamed=$($renameResult.Renamed)|collisions=$($renameResult.Collisions)|errors=$($renameResult.Errors)" -ForegroundColor Cyan
}
2025-11-06 10:47:19 -07:00
$filesRenamedTotal += $renameResult.Renamed
$renameCollisions += $renameResult.Collisions
$renameErrors += $renameResult.Errors
2025-11-06 10:47:19 -07:00
if ($renameResult.FrameCount -gt 0 -and $renameResult.MinFrame -ne $null -and $renameResult.MaxFrame -ne $null) {
$mp4Result = Rename-SequencePreviewMp4 -SequenceFolderPath $seq.FullName -SequenceName $seq.Name -StartFrame $renameResult.MinFrame -EndFrame $renameResult.MaxFrame -LogFile $logFile
if ($DebugMode -or $mp4Result.Renamed -gt 0 -or $mp4Result.Collisions -gt 0 -or $mp4Result.Errors -gt 0) {
Write-Host "[MP4]|$($seq.FullName)|$($seq.Name)|renamed=$($mp4Result.Renamed)|collisions=$($mp4Result.Collisions)|errors=$($mp4Result.Errors)" -ForegroundColor Cyan
}
2025-11-06 10:47:19 -07:00
$mp4RenamedTotal += $mp4Result.Renamed
$mp4Collisions += $mp4Result.Collisions
$mp4Errors += $mp4Result.Errors
}
}
Write-Host "=== SUMMARY REPORT ===" -ForegroundColor Magenta
2025-11-06 10:47:19 -07:00
Write-Host "Sequences scanned: $totalSequences" -ForegroundColor White
Write-Host "Files renamed: $filesRenamedTotal (collisions: $renameCollisions, errors: $renameErrors)" -ForegroundColor White
2025-10-23 10:33:23 -06:00
Write-Host "Preview MP4s renamed: $mp4RenamedTotal (collisions: $mp4Collisions, errors: $mp4Errors)" -ForegroundColor White
2025-09-16 22:11:55 -06:00
Write-Host "=====================" -ForegroundColor Magenta
2025-11-06 10:47:19 -07:00
if ($logFile) {
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] === UpdateSequences completed (seq=$totalSequences renamed=$filesRenamedTotal mp4=$mp4RenamedTotal) ===" | Add-Content -LiteralPath $logFile
}
exit 0
}
catch {
2025-11-06 10:47:19 -07:00
if ($logFile) {
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] ERROR: $($_.Exception.Message)" | Add-Content -LiteralPath $logFile
}
2025-11-06 10:47:19 -07:00
Write-Host "ERROR: $_" -ForegroundColor Red
exit 1
2025-09-16 22:11:55 -06:00
}