Skip to content

Instantly share code, notes, and snippets.

@datalogics-robl
Created June 21, 2019 15:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save datalogics-robl/1b60ab6680a2e1dcd97cd8589418e6b4 to your computer and use it in GitHub Desktop.
Save datalogics-robl/1b60ab6680a2e1dcd97cd8589418e6b4 to your computer and use it in GitHub Desktop.
PowerShell hot folder script for converting files with FLIP2PDF
param (
[Parameter(Mandatory=$true)]$source,
[Parameter(Mandatory=$true)]$destination,
$logpath = $destination + "\convertfile.log"
)
$destination += "\"
# Function to write to our log file
function Write-Log {
param($message)
"$(Get-Date -Format G) : $message" | Out-File -FilePath $logpath -Append -Force
}
$fsw = New-Object IO.FileSystemWatcher $source, "*" -Property @{
IncludeSubdirectories = $true
NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'
}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileConverted -Action {
$path = $Event.SourceEventArgs.FullPath
$name = $Event.SourceEventArgs.Name
$extension = [System.IO.Path]::GetExtension($name).ToLower()
$outputName = $destination + [System.IO.Path]::GetFileNameWithoutExtension($name) + ".pdf"
# Ignore lock files or JSON configuration files
if ($name.StartsWith(".~lock.") -or $extension -eq ".json") {
return
}
# Use profile if available
$profileKeyword = $profile = ""
if (Test-Path "$source\profile.json") {
$profileKeyword = "--profile"
$profile = "$source\profile.json"
}
$result = Invoke-Command { flip2pdf --input $path --output $outputName $profileKeyword $profile }
Move-Item $path -Destination $destination -Force # Force will overwrite files with the same name
# Log results
if ($LASTEXITCODE -eq 0) {
Write-Log "$name successfully converted to $outputName"
} else {
Write-Log "Conversion of $name failed with error code: $LASTEXITCODE, message: $($result[5])"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment