Automatically Split PDF Documents using a Hot-Folder
- Introduction
- It is often convenient to a monitor a specific folder (called a “hot-folder”) and automatically process every new file placed into it. This way there is no need to manually run any batch file or application, the files are getting processed dynamically, as they are being placed into the “hot-folder”.
- This tutorial is going to focus on how to monitor a folder and process the files. Please see a previous tutorial on details how to configure AutoSplit to process PDF files and how to create a command-line BAT file..
- Prerequisites
- You need a copy of Adobe® Acrobat® along with the AutoSplit plug-in installed on your computer in order to use this tutorial. Both are available as trial versions.
- Overview
- In this tutorial we will use the following files:
- SplitProfile.apr - Auto-Split document splitting settings file.
- HotFolder.BAT - a batch file that starts Adobe Acrobat and applies SplitProfile.apr to the input file.
- WatchFolder.ps1 - a Powershell script that monitors a folder and runs HotFolder.BAT on each new file.
- We will use these 3 files to monitor a folder and split every file that is copied into it.
- Creating Splitting Settings File
- Open a sample PDF document and select "Plug-ins > Split Documents > Split Document" from the menu. Use any of the available splitting methods to configure a splitting job. Refer to the step-by-step tutorials for details on various splitting methods that are provided by the AutoSplit Pro plug-in. Test the settings on a sample PDF file to make sure they work correctly and produce desired results.
- It is a good idea to configure the settings to store output files into a different folder than the "hot-folder" one. Placing the output files right into the hot-folder will trigger to splitting for each of them.
- Once you have a working splitting settings, press "Save Profile..." button located on the main "Split Document" screen to save them as SplitProfile.apr file..
- BAT File Syntax
- We would need to create a batch file that can accept path to the input file as a parameter. This way the folder-monitoring script or application can pass a filename that needs to be processed to the BAT file. Use SET AUTOSPLIT_INPUT_FILE=%1 to specify the input filename. Note that %1 refers to a first command-line parameter that is passed to the BAT file during the excution.
- Here is the HotFolder.BAT file we are going to use. It starts the Adobe Acrobat DC and applies "Split Document" operation to a PDF file that is passed to it as a parameter:
-
SET AUTOSPLIT_INPUT_FILE=%1
SET AUTOSPLIT_CONFIG_FILE=C:\Data\SplitProfile.apr
SET AUTOSPLIT_BAT_ENABLE=ON
SET AUTOSPLIT_MODE=Split
SET AUTOSPLIT_LOG_FILE=C:\Data\HotFolder\AutoSplitLog.txt
"C:\Program Files (x86)\Adobe\Acrobat DC\Acrobat\Acrobat.exe" /n /h
- Monitoring a Folder using a Powershell Script
- There are many different ways to monitor a folder. We are going to show how to use a Powershell script to do that. PowerShell is a task automation and configuration management framework from Microsoft, consisting of a command-line shell and associated scripting language. The script below is an adaptation of the method explained in the following excelent online article. Refer to this page for in-depth details and discussions on the Powershell programming methods.
- Here is the Powershell script that monitors the c:\Data\HotFolder folder and runs HotFolder.BAT file on every file that is placed into it. Copy the following code into a text file and save it with *.ps1 file extension (for example: WatchFolder.ps1). If you want to monitor a different folder or specify a different BAT file, then make changes accordingly.
- Note that files placed into a "hot-folder" will remain there after the splitting. If you want to move them into a different location after the processing, then modify the Powershell script accordingly.
- Important: The following script is provided "as-is" without warranty of any kind. Use it at your own risk. It is provided as an example only.
-
### This script monitors a hot-folder and starts a BAT file on every new file placed into it ### Replace the following variables with your own values ### $Path - full file path to the hot-folder ### $logPath = "C:\Data\HotFolder\LogFile.txt" - path to the log file ### c:\data\HotFolder.BAT - full file path to the BAT file to execute try { ### SET FOLDER TO WATCH $Path = "C:\Data\HotFolder" ### SET FILE TYPES TO WATCH + SUBFOLDERS YES/NO $watcher = New-Object System.IO.FileSystemWatcher $watcher.Path = $Path $watcher.Filter = "*.pdf" $watcher.IncludeSubdirectories = $false ### RUN BAT FILE AND PASS FILENAME PARAMETER $action = { $path = $Event.SourceEventArgs.FullPath $changeType = $Event.SourceEventArgs.ChangeType $logline = "$(Get-Date), $changeType, $path" ### add a record to a log file $logPath = "C:\Data\HotFolder\LogFile.txt" Add-Content $logPath -value $logline c:\data\HotFolder.BAT $path -Verb runas } ### WATCH FOR NEW FILES CREATED IN THE FOLDER $handlers = . { Register-ObjectEvent $watcher "Created" -Action $action } Write-Host "Watching for new files in $Path" $watcher.EnableRaisingEvents = $true do { # Wait-Event waits for a second and stays responsive to events # Start-Sleep in contrast would NOT work and ignore incoming events Wait-Event -Timeout 5 # write a dot to indicate we are still monitoring: Write-Host "." -NoNewline } while ($true) } finally { # this gets executed when user presses CTRL+C: # stop monitoring $watcher.EnableRaisingEvents = $false # remove the event handlers $handlers | ForEach-Object { Unregister-Event -SourceIdentifier $_.Name } # event handlers are technically implemented as a special kind # of background job, so remove the jobs now: $handlers | Remove-Job # properly dispose the FileSystemWatcher: $watcher.Dispose() Write-Warning "Stop folder monitoring." }
- How to Start and Stop Monitoring
-
- Create HotFolder.BAT file and splitting settings file (SplitProfile.apr).
- Create and save WatchFolder.ps1 file based on the script above.
- Right-click on the WatchFolder.ps1 in Windows Explorer and select "Run with Powershell" from a popup menu.
- Powershell window will appear on screen, monitoring is active now.
- Press Ctrl+C or simply close Powershell window to stop monitoring.
- The script will output a dot every 5 seconds to indicate that it is live and working.
- Log Files
- The script will also create and maintain a log file (C:\Data\HotFolder\LogFile.txt) that records time when a new file has been placed into a folder. Edit the script if you do not need this functionality.
- The HotFolder.BAT file executed by folder-monitoring script will also create a separate log file that records every file that has been processed. The log file shows the input filenames that have been successfully processed. It will also record any errors encountered during the execution. Note, that some PDF files cannot be processed due to the security settings or password-protection.
-
17/05/2020 , 05:06:27: Batch processing mode: Split 17/05/2020 , 05:06:27: Using log file: C:\Data\HotFolder\AutoSplitLog.txt 17/05/2020 , 05:06:27: Settings file C:\Data\SplitProfile.apr 17/05/2020 , 05:06:27: Input PDF file C:\Data\HotFolder\InvoicesBatch2345.pdf 17/05/2020 , 05:07:17: Batch processing mode: Split 17/05/2020 , 05:07:17: Using log file: C:\Data\HotFolder\AutoSplitLog.txt 17/05/2020 , 05:07:17: Settings file C:\Data\SplitProfile.apr 17/05/2020 , 05:07:17: Input PDF file C:\Data\HotFolder\Binder 3 Pages.pdf 17/05/2020 , 05:07:36: Batch processing mode: Split 17/05/2020 , 05:07:36: Using log file: C:\Data\HotFolder\AutoSplitLog.txt 17/05/2020 , 05:07:36: Settings file C:\Data\SplitProfile.apr 17/05/2020 , 05:07:36: Input PDF file C:\Data\HotFolder\InvoicesRenewals2020.pdf 17/05/2020 , 05:16:36: Batch processing mode: Split 17/05/2020 , 05:16:36: Using log file: C:\Data\HotFolder\AutoSplitLog.txt 17/05/2020 , 05:16:36: Settings file C:\Data\SplitProfile.apr 17/05/2020 , 05:16:36: Input PDF file C:\Data\HotFolder\Invoice - 19690_Secured.pdf 17/05/2020 , 05:16:36: Failed to open input PDF file C:\Data\HotFolder\Invoice - 19690_Secured.pdf
- You can find more AutoSplit tutorials here.