updateseq rename seq based on folder name
This commit is contained in:
@@ -17,6 +17,70 @@ function Get-YoungestTimestamp {
|
||||
return $young
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
$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 }
|
||||
}
|
||||
}
|
||||
|
||||
return [pscustomobject]@{
|
||||
Renamed = $renamed
|
||||
Collisions = $collisions
|
||||
Errors = $errors
|
||||
Checked = $checked
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
$root = (Get-Location).ProviderPath
|
||||
$currentDir = Join-Path $root '_CURRENT'
|
||||
@@ -32,12 +96,22 @@ try {
|
||||
|
||||
$mapDaily = @{}
|
||||
$dailiesScanned = 0
|
||||
$filesRenamedTotal = 0
|
||||
$renameCollisions = 0
|
||||
$renameErrors = 0
|
||||
foreach ($d in $dailyDirs) {
|
||||
$dailiesScanned++
|
||||
$seqDirs = @(Get-ChildItem -LiteralPath $d.FullName -Directory | Where-Object { $_.Name -ne '_archive' })
|
||||
if ($seqDirs.Count -eq 0) { $seqDirs = @($d) }
|
||||
foreach ($s in $seqDirs) {
|
||||
if (-not (Test-Path -LiteralPath $s.FullName)) { continue }
|
||||
$renameResult = Sync-SequenceFilenames -SequenceFolderPath $s.FullName -SequenceName $s.Name -LogFile $logFile
|
||||
if ($DebugMode -or $renameResult.Renamed -gt 0 -or $renameResult.Collisions -gt 0 -or $renameResult.Errors -gt 0) {
|
||||
Write-Host "[RENAME]|$($s.FullName)|$($s.Name)|checked=$($renameResult.Checked)|renamed=$($renameResult.Renamed)|collisions=$($renameResult.Collisions)|errors=$($renameResult.Errors)" -ForegroundColor Cyan
|
||||
}
|
||||
$filesRenamedTotal += $renameResult.Renamed
|
||||
$renameCollisions += $renameResult.Collisions
|
||||
$renameErrors += $renameResult.Errors
|
||||
$young = Get-YoungestTimestamp -FolderPath $s.FullName
|
||||
$filesCount = @(Get-ChildItem -LiteralPath $s.FullName -Recurse -File -ErrorAction SilentlyContinue | Where-Object { $_.FullName -notlike '*\_archive\*' }).Count
|
||||
$seqName = $s.Name
|
||||
@@ -102,12 +176,12 @@ try {
|
||||
$dstAbs = Join-Path $currentDir $seqName
|
||||
if (-not (Test-Path -LiteralPath $dstAbs)) { New-Item -ItemType Directory -Path $dstAbs -Force | Out-Null }
|
||||
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] Mirror: '$srcFull' -> '$dstAbs'" | Add-Content -LiteralPath $logFile
|
||||
$args = @(
|
||||
$robocopyArgs = @(
|
||||
$srcFull,
|
||||
$dstAbs,
|
||||
'/MIR','/MT:8','/R:1','/W:1','/COPY:DAT','/DCOPY:DAT','/FFT','/NFL','/NDL','/NP','/NJH','/NJS','/XD','_archive'
|
||||
)
|
||||
$null = & robocopy @args 2>&1 | Add-Content -LiteralPath $logFile
|
||||
$null = & robocopy @robocopyArgs 2>&1 | Add-Content -LiteralPath $logFile
|
||||
$rc = $LASTEXITCODE
|
||||
if ($rc -lt 8) {
|
||||
$sequencesMirrored++
|
||||
@@ -126,6 +200,7 @@ try {
|
||||
Write-Host "Planned copies: $toCopy" -ForegroundColor Green
|
||||
Write-Host "Completed OK: $sequencesMirrored" -ForegroundColor Green
|
||||
Write-Host "Completed FAIL: $mirrorFailures" -ForegroundColor Red
|
||||
Write-Host "Files renamed: $filesRenamedTotal (collisions: $renameCollisions, errors: $renameErrors)" -ForegroundColor White
|
||||
|
||||
# Summarize skipped sequences
|
||||
$skippedLines = @()
|
||||
|
||||
Reference in New Issue
Block a user