53 lines
2.0 KiB
PowerShell
53 lines
2.0 KiB
PowerShell
param(
|
|
[string]$PdfRoot = 'C:\inetpub\wwwroot\pdf\2026',
|
|
[string]$MapCsv = 'C:\inetpub\wwwroot\cmod\mapping acc to host.csv',
|
|
[string]$LogCsv = 'C:\inetpub\wwwroot\cmod\pdf_rename_2026_log.csv'
|
|
)
|
|
|
|
$rows = Import-Csv $MapCsv -Delimiter "`t"
|
|
$map = @{}
|
|
foreach ($r in $rows) {
|
|
$acc = ($r.SP_ACCESSNUMBER + '').Trim()
|
|
$hostOrder = ($r.HOSTORDERNUMBER + '').Trim()
|
|
if ($acc -and $hostOrder) { $map[$acc] = $hostOrder }
|
|
}
|
|
|
|
$files = Get-ChildItem -Path $PdfRoot -Recurse -File -Filter '*.pdf'
|
|
$latest = @{}
|
|
foreach ($f in $files) {
|
|
if ($f.BaseName -notmatch '^(?<acc>.+?)(?<suffix>_eng)?$') { continue }
|
|
$acc = $Matches.acc
|
|
$suffix = $Matches.suffix
|
|
if (-not $map.ContainsKey($acc)) { continue }
|
|
$targetName = '{0}{1}.pdf' -f $map[$acc], $suffix
|
|
$targetPath = Join-Path $f.DirectoryName $targetName
|
|
if (-not $latest.ContainsKey($targetPath) -or $f.LastWriteTime -gt $latest[$targetPath].LastWriteTime) {
|
|
$latest[$targetPath] = $f
|
|
}
|
|
}
|
|
|
|
$log = foreach ($targetPath in $latest.Keys) {
|
|
$src = $latest[$targetPath]
|
|
if ($src.BaseName -notmatch '^(?<acc>.+?)(?<suffix>_eng)?$') { continue }
|
|
$acc = $Matches.acc
|
|
$suffix = $Matches.suffix
|
|
$hostOrder = $map[$acc]
|
|
$targetName = '{0}{1}.pdf' -f $hostOrder, $suffix
|
|
$finalTarget = Join-Path $src.DirectoryName $targetName
|
|
|
|
try {
|
|
if (Test-Path $finalTarget) { Remove-Item -Path $finalTarget -Force }
|
|
if ($src.FullName -ne $finalTarget) {
|
|
Move-Item -Path $src.FullName -Destination $finalTarget -Force
|
|
[pscustomobject]@{Status='RENAMED'; Source=$src.FullName; Target=$finalTarget; AccessNumber=$acc; HostOrderNumber=$hostOrder; Notes=''}
|
|
} else {
|
|
[pscustomobject]@{Status='UNCHANGED'; Source=$src.FullName; Target=$finalTarget; AccessNumber=$acc; HostOrderNumber=$hostOrder; Notes='Already correct name'}
|
|
}
|
|
} catch {
|
|
[pscustomobject]@{Status='ERROR'; Source=$src.FullName; Target=$finalTarget; AccessNumber=$acc; HostOrderNumber=$hostOrder; Notes=$_.Exception.Message}
|
|
}
|
|
}
|
|
|
|
$log | Export-Csv -Path $LogCsv -NoTypeInformation -Encoding UTF8
|
|
Write-Host "Done. Log: $LogCsv"
|