Use today's YYYYMMDD subfolder for PDFs, archive whole folder on success

This commit is contained in:
2026-06-29 19:47:38 -05:00
parent a26eb39ccb
commit 54baaf52e7
3 changed files with 87 additions and 64 deletions
+30 -21
View File
@@ -1,16 +1,23 @@
# FTP/FTPS PDF Uploader
# Place this script and ftp-config.ini in the same folder as your PDFs.
# Place this script and ftp-config.ini in the practice folder.
# PDFs should be in a subfolder named with today's date (YYYYMMDD).
# Run via upload.bat (double-click).
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$configFile = Join-Path $scriptDir "ftp-config.ini"
$today = Get-Date -Format "yyyyMMdd"
$dateDir = Join-Path $scriptDir $today
if (-not (Test-Path $configFile)) {
Write-Host "ERROR: ftp-config.ini not found." -ForegroundColor Red
Write-Host "Create ftp-config.ini in the same folder as this script." -ForegroundColor Red
exit 1
}
if (-not (Test-Path $dateDir)) {
Write-Host "No folder for today ($today) found in this practice folder." -ForegroundColor Yellow
exit 0
}
# Parse config
$config = @{}
foreach ($line in Get-Content $configFile) {
@@ -36,16 +43,16 @@ if (-not (Test-Path $archiveDir)) {
New-Item -ItemType Directory -Path $archiveDir | Out-Null
}
$pdfs = Get-ChildItem -Path $scriptDir -Filter "*.pdf" -File
$pdfs = Get-ChildItem -Path $dateDir -Filter "*.pdf" -File
if ($pdfs.Count -eq 0) {
Write-Host "No PDF files found in this folder." -ForegroundColor Yellow
Write-Host "No PDF files found in $today\" -ForegroundColor Yellow
exit 0
}
$protocol = if ($useTls) { "ftps" } else { "ftp" }
Write-Host ""
Write-Host "Uploading $($pdfs.Count) PDF(s) to $protocol://$ftpHost$remotePath" -ForegroundColor Cyan
Write-Host "Uploading $($pdfs.Count) PDF(s) from $today\ to $protocol://$ftpHost$remotePath" -ForegroundColor Cyan
Write-Host ""
$ok = 0
@@ -58,24 +65,23 @@ foreach ($pdf in $pdfs) {
Write-Host " $($pdf.Name) ..." -NoNewline
try {
$req = [System.Net.FtpWebRequest]::Create($uri)
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$req.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$req.EnableSsl = $useTls
$req.UseBinary = $true
$req.UsePassive = $true
$req.KeepAlive = $false
$req = [System.Net.FtpWebRequest]::Create($uri)
$req.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$req.Credentials = New-Object System.Net.NetworkCredential($username, $password)
$req.EnableSsl = $useTls
$req.UseBinary = $true
$req.UsePassive = $true
$req.KeepAlive = $false
$bytes = [System.IO.File]::ReadAllBytes($pdf.FullName)
$req.ContentLength = $bytes.Length
$stream = $req.GetRequestStream()
$bytes = [System.IO.File]::ReadAllBytes($pdf.FullName)
$req.ContentLength = $bytes.Length
$stream = $req.GetRequestStream()
$stream.Write($bytes, 0, $bytes.Length)
$stream.Close()
$resp = $req.GetResponse()
$resp.Close()
Move-Item -Path $pdf.FullName -Destination (Join-Path $archiveDir $pdf.Name) -Force
Write-Host " uploaded" -ForegroundColor Green
$ok++
}
@@ -86,8 +92,11 @@ foreach ($pdf in $pdfs) {
}
Write-Host ""
if ($fail -eq 0) {
Write-Host "Done: $ok file(s) uploaded and archived." -ForegroundColor Green
} else {
Write-Host "Done: $ok uploaded, $fail failed (left in place for retry)." -ForegroundColor Yellow
# Archive the whole dated folder if everything succeeded
if ($fail -eq 0 -and $ok -gt 0) {
Move-Item -Path $dateDir -Destination (Join-Path $archiveDir $today) -Force
Write-Host "Done: $ok file(s) uploaded. Folder $today\ moved to archive." -ForegroundColor Green
} elseif ($fail -gt 0) {
Write-Host "Done: $ok uploaded, $fail failed (left in $today\ for retry)." -ForegroundColor Yellow
}