103 lines
3.3 KiB
PowerShell
103 lines
3.3 KiB
PowerShell
# FTP/FTPS PDF Uploader
|
|
# 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
|
|
$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
|
|
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) {
|
|
if ($line -match '^\s*([^=;#\s]+)\s*=\s*(.+?)\s*$') {
|
|
$config[$matches[1]] = $matches[2]
|
|
}
|
|
}
|
|
|
|
$ftpHost = $config["host"]
|
|
$ftpPort = if ($config["port"]) { [int]$config["port"] } else { 21 }
|
|
$username = $config["username"]
|
|
$password = $config["password"]
|
|
$remotePath = if ($config["remote_path"]) { $config["remote_path"].TrimEnd("/") } else { "/" }
|
|
$useTls = $config["tls"] -eq "true"
|
|
|
|
if (-not $ftpHost -or -not $username -or -not $password) {
|
|
Write-Host "ERROR: ftp-config.ini is missing required fields (host, username, password)." -ForegroundColor Red
|
|
exit 1
|
|
}
|
|
|
|
$archiveDir = Join-Path $scriptDir "archive"
|
|
if (-not (Test-Path $archiveDir)) {
|
|
New-Item -ItemType Directory -Path $archiveDir | Out-Null
|
|
}
|
|
|
|
$pdfs = Get-ChildItem -Path $dateDir -Filter "*.pdf" -File
|
|
|
|
if ($pdfs.Count -eq 0) {
|
|
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) from $today\ to $protocol://$ftpHost$remotePath" -ForegroundColor Cyan
|
|
Write-Host ""
|
|
|
|
$ok = 0
|
|
$fail = 0
|
|
|
|
foreach ($pdf in $pdfs) {
|
|
$remoteFile = "$remotePath/$($pdf.Name)"
|
|
$uri = "ftp://${ftpHost}:${ftpPort}${remoteFile}"
|
|
|
|
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
|
|
|
|
$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()
|
|
|
|
Write-Host " uploaded" -ForegroundColor Green
|
|
$ok++
|
|
}
|
|
catch {
|
|
Write-Host " FAILED: $($_.Exception.Message)" -ForegroundColor Red
|
|
$fail++
|
|
}
|
|
}
|
|
|
|
Write-Host ""
|
|
|
|
# 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
|
|
}
|