Initial upload script — PDF to FTP with archive
This commit is contained in:
+93
@@ -0,0 +1,93 @@
|
||||
# FTP/FTPS PDF Uploader
|
||||
# Place this script and ftp-config.ini in the same folder as your PDFs.
|
||||
# Run via upload.bat (double-click).
|
||||
|
||||
$scriptDir = Split-Path -Parent $MyInvocation.MyCommand.Path
|
||||
$configFile = Join-Path $scriptDir "ftp-config.ini"
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
# 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 $scriptDir -Filter "*.pdf" -File
|
||||
|
||||
if ($pdfs.Count -eq 0) {
|
||||
Write-Host "No PDF files found in this folder." -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 ""
|
||||
|
||||
$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()
|
||||
|
||||
Move-Item -Path $pdf.FullName -Destination (Join-Path $archiveDir $pdf.Name) -Force
|
||||
Write-Host " uploaded" -ForegroundColor Green
|
||||
$ok++
|
||||
}
|
||||
catch {
|
||||
Write-Host " FAILED: $($_.Exception.Message)" -ForegroundColor Red
|
||||
$fail++
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user