Auto-Renaming PDF Files using a Hot-Folder
AutoSplit Pro plug-in for Adobe® Acrobat®
- Introduction
- It is often convenient to a monitor a specific folder (called a “hot-folder”) and automatically rename every new file placed into it. This way there is no need to manually run any batch file or application, the files are getting renamed dynamically, as they are being placed into the “hot-folder”.
- This tutorial is going to focus entirely on how to monitor a folder and process the files. Please see a previous tutorial on details how to configure AutoSplit to auto-rename 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.
Step-by-Step Tutorial
- Overview
- In this tutorial we will use the following files:
- RenamingSettings.docren - Auto-Split auto-renaming settings.
- HotFolder.BAT - a batch file that starts Adobe Acrobat and applies RenamingSettings.docren to the input file.
- WatchFolder.ps1 - a Powershell script that monitors a folder and runs HotFolder.BAT on each new file.
- BAT File Syntax
- We would need to make a single change to the BAT file created in the previous tutorial and make sure it can accept a filename as a parameter that is passed to the BAT file. This way the folder-monitoring script or application can pass a filename that needs to be processed. 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 Adobe Acrobat and runs an auto-renaming operation on a PDF file that is passed to it as a parameter:
-
SET AUTOSPLIT_INPUT_FOLDER=%1
SET AUTOSPLIT_CONFIG_FILE=C:\Data\RenamingSettings.docren
SET AUTOSPLIT_BAT_ENABLE=ON
SET AUTOSPLIT_MODE=RenameFile
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 copied 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.
- Important: The following script is provided "as-is" without warranty of any kind. Use it at your own risk.
-
### 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 and save WatchFolder.ps1 file based on the script example 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 old and new file name for every file that have been successfully processed. It will also record any errors encountered during the execution. Note, that some PDF files cannot be renamed due to the security settings or password-protection.
- You can find more AutoSplit tutorials here.