convert to ps1 script and debug

This commit is contained in:
Nathan
2025-08-18 17:04:19 -06:00
parent d276607178
commit e3fea9d423
3 changed files with 3221 additions and 77 deletions

View File

@@ -1,81 +1,12 @@
@echo off
setlocal EnableExtensions EnableDelayedExpansion
setlocal EnableExtensions
set "rootDir=%CD%"
set "currentDir=%rootDir%\_CURRENT"
if not exist "%currentDir%" (
mkdir "%currentDir%" >nul 2>&1
if not exist "%currentDir%" (
echo ERROR: Cannot create "%currentDir%" permission denied?
echo Run the script with sufficient rights or choose a writable location.
exit /b 1
)
)
set "logFile=%currentDir%\_UpdateSequences.log"
echo [%date% %time%] === UpdateSequences run started in "%rootDir%" ===>> "%logFile%"
set /a dailiesScanned=0
set /a sequencesTotal=0
set /a sequencesMirrored=0
set /a mirrorFailures=0
for /d %%D in (daily_*) do (
if exist "%%D" (
rem Skip _archive folders
if not "%%~nxD"=="_archive" (
set /a dailiesScanned+=1
set "hadSub=0"
rem Case 1: subfolders treated as sequences
for /d %%S in ("%%D\*") do (
set /a sequencesTotal+=1
set "hadSub=1"
call :MirrorOne "%%D\%%~nxS" "%%~nxS"
)
rem Case 2: no subfolders => mirror daily itself as a sequence
if "!hadSub!"=="0" (
set /a sequencesTotal+=1
call :MirrorOne "%%D" "%%~nxD"
)
) else (
echo Skip: archive folder "%%D"
)
)
)
echo( Summary: dailies=%dailiesScanned% sequences=%sequencesTotal% ok=%sequencesMirrored% fail=%mirrorFailures%
echo [%date% %time%] === UpdateSequences run completed (d=%dailiesScanned% seq=%sequencesTotal% ok=%sequencesMirrored% fail=%mirrorFailures%) ===>> "%logFile%"
set "ps1=A:\1 Amazon_Active_Projects\3 ProjectStructure\UpdateSequences.ps1"
echo Running PowerShell update script...
powershell -NoProfile -ExecutionPolicy Bypass -File "%ps1%"
set "rc=%errorlevel%"
echo PowerShell exited with RC=%rc%
echo @"_CURRENT\_UpdateSequences.log"
echo( Done. Press any key to exit.
pause >nul
exit /b 0
:MirrorOne
setlocal EnableExtensions EnableDelayedExpansion
set "srcRel=%~1"
set "seqName=%~2"
set "srcAbs=%rootDir%\%srcRel%"
set "dstAbs=%currentDir%\%seqName%"
rem Only proceed if source is a directory (trailing \ check)
if not exist "%srcRel%\" (
echo Skip: missing folder "%srcRel%"
endlocal & exit /b 0
)
echo Mirror: "%srcAbs%" -^> "%dstAbs%"
echo [%date% %time%] Mirror: "%srcAbs%" -^> "%dstAbs%" >> "%logFile%"
robocopy "%srcAbs%" "%dstAbs%" /MIR /MT:8 /R:1 /W:1 /COPY:DAT /DCOPY:DAT /FFT /NFL /NDL /NP /NJH /NJS >> "%logFile%" 2>&1
set "rc=!errorlevel!"
if "!rc!"=="" set "rc=0"
if !rc! LSS 8 (
set /a sequencesMirrored+=1
echo( OK (rc=!rc!)
echo [%date% %time%] OK rc=!rc! >> "%logFile%"
) else (
set /a mirrorFailures+=1
echo( ERROR (rc=!rc!) - see log
echo [%date% %time%] ERROR rc=!rc! >> "%logFile%"
)
endlocal & exit /b 0
exit /b %rc%

178
UpdateSequences.ps1 Normal file
View File

@@ -0,0 +1,178 @@
[CmdletBinding()]
param(
[switch]$DebugMode
)
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Get-YoungestTimestamp {
param(
[Parameter(Mandatory)] [string]$FolderPath
)
$files = Get-ChildItem -LiteralPath $FolderPath -Recurse -File -ErrorAction SilentlyContinue |
Where-Object { $_.FullName -notlike '*\_archive\*' }
$young = $files | Sort-Object LastWriteTime -Descending | Select-Object -First 1 -ExpandProperty LastWriteTime
if (-not $young) { return [datetime]::Parse('1900-01-01') }
return $young
}
try {
$root = (Get-Location).ProviderPath
$currentDir = Join-Path $root '_CURRENT'
if (-not (Test-Path -LiteralPath $currentDir)) {
New-Item -ItemType Directory -Path $currentDir -Force | Out-Null
}
$logFile = Join-Path $currentDir '_UpdateSequences.log'
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] === UpdateSequences (ps1) started in '$root' ===" | Add-Content -LiteralPath $logFile
$dailyDirs = Get-ChildItem -LiteralPath $root -Directory -Filter 'daily_*' | Where-Object { $_.Name -ne '_archive' }
$mapDaily = @{}
$dailiesScanned = 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 }
$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
$cand = [pscustomobject]@{
Seq = $seqName
SrcFull = $s.FullName
Young = $young
FilesCount = $filesCount
}
if ($mapDaily.ContainsKey($seqName)) {
if ($mapDaily[$seqName].Young -lt $young) { $mapDaily[$seqName] = $cand }
} else {
$mapDaily[$seqName] = $cand
}
}
}
$mapCurrent = @{}
if (Test-Path -LiteralPath $currentDir) {
foreach ($c in Get-ChildItem -LiteralPath $currentDir -Directory | Where-Object { $_.Name -ne '_archive' }) {
$mapCurrent[$c.Name] = Get-YoungestTimestamp -FolderPath $c.FullName
}
}
$lines = @()
$lines += "[MAPCOUNT]|count=$($mapDaily.Count)"
$total = 0
$toCopy = 0
foreach ($k in $mapDaily.Keys) {
$total++
$d = $mapDaily[$k]
$curYoung = if ($mapCurrent.ContainsKey($k)) { $mapCurrent[$k] } else { [datetime]::Parse('1900-01-01') }
if ($d.Young -gt $curYoung) {
$toCopy++
$lines += "[COPY]|$($d.SrcFull)|$($d.Seq)|files=$($d.FilesCount)|young=$($d.Young.ToString('s'))"
} else {
$lines += "[SKIP]|$($d.SrcFull)|$($d.Seq)|reason=notNewer|srcYoung=$($d.Young.ToString('s'))|curYoung=$($curYoung.ToString('s'))"
}
}
$lines += "[META]|dailiesScanned=$dailiesScanned|sequencesTotal=$total|toCopy=$toCopy"
# Print plan to console instead of writing to file
Write-Host "=== UPDATE PLAN ===" -ForegroundColor Cyan
foreach ($line in $lines) {
if ($line -like '[COPY]*') {
Write-Host $line -ForegroundColor Green
} elseif ($line -like '[SKIP]*') {
Write-Host $line -ForegroundColor Yellow
} else {
Write-Host $line -ForegroundColor White
}
}
Write-Host "==================" -ForegroundColor Cyan
$sequencesMirrored = 0
$mirrorFailures = 0
foreach ($line in $lines) {
if ($line.StartsWith('[COPY]')) {
$parts = $line -split '\|'
$srcFull = $parts[1]
$seqName = $parts[2]
$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 = @(
$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
$rc = $LASTEXITCODE
if ($rc -lt 8) {
$sequencesMirrored++
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] OK rc=$rc" | Add-Content -LiteralPath $logFile
} else {
$mirrorFailures++
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] ERROR rc=$rc" | Add-Content -LiteralPath $logFile
}
}
}
# Print summary report to console
Write-Host "=== SUMMARY REPORT ===" -ForegroundColor Magenta
Write-Host "Dailies scanned: $dailiesScanned" -ForegroundColor White
Write-Host "Sequences found: $total" -ForegroundColor White
Write-Host "Planned copies: $toCopy" -ForegroundColor Green
Write-Host "Completed OK: $sequencesMirrored" -ForegroundColor Green
Write-Host "Completed FAIL: $mirrorFailures" -ForegroundColor Red
# Summarize skipped sequences
$skippedLines = @()
foreach ($line in $lines) {
if ($line.StartsWith('[SKIP]')) {
$skippedLines += $line
}
}
if ($skippedLines.Count -gt 0) {
Write-Host "`n=== SKIPPED SEQUENCES ===" -ForegroundColor Yellow
$skippedByReason = @{}
foreach ($skip in $skippedLines) {
$parts = $skip -split '\|'
$reason = $parts[3]
$seqName = $parts[2]
if (-not $skippedByReason.ContainsKey($reason)) {
$skippedByReason[$reason] = @()
}
$skippedByReason[$reason] += $seqName
}
foreach ($reason in $skippedByReason.Keys) {
$seqs = $skippedByReason[$reason]
Write-Host "$reason ($($seqs.Count) sequences):" -ForegroundColor Yellow
foreach ($seq in $seqs | Sort-Object) {
Write-Host " - $seq" -ForegroundColor White
}
}
Write-Host "========================" -ForegroundColor Yellow
}
Write-Host "=====================" -ForegroundColor Magenta
"[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] === UpdateSequences (ps1) completed (d=$dailiesScanned seq=$total ok=$sequencesMirrored fail=$mirrorFailures) ===" | Add-Content -LiteralPath $logFile
Write-Host "@$logFile"
exit 0
}
catch {
$root = (Get-Location).ProviderPath
$currentDir = Join-Path $root '_CURRENT'
if (-not (Test-Path -LiteralPath $currentDir)) {
New-Item -ItemType Directory -Path $currentDir -Force | Out-Null
}
$planFile = Join-Path $currentDir '_UpdatePlan.txt'
($_.Exception | Out-String) | Set-Content -LiteralPath ($planFile + '.error.txt') -Encoding ASCII
Write-Host "ERROR: $_"
exit 1
}