I have a legacy DOS App (no negative comments, there is a long and genuine story to why we are still running this!) that can print to file in PostScript format. I am successfully using the Open Source Ghostscript tool to convert to PDF (specifically calling on the 'ps2pdf' command in the Ghostscript directory, which generates a perfect PDF file).
I've tried creating a PowerShell script to automate this, which constantly monitors a specific folder and if a PostScript (*.ps) file appears in the set directory, works the conversion magic and loads up Adobe Reader with the completed PDF.
Here's the problem; the script only works 9 out of 10 times. I think the problem is the DOS App immediately generates a 0 kb .ps file when printing to file, and then pads out the content after completing generation in memory. It typically takes about 10 seconds to do the actual PostScript content generation, and then writes to the .ps file the content. As far as I can tell, it puts no lock on the file whilst this all happens. I've used the 'FileChanged' type command in my script, so most the time it ignores the initial .ps as it is created, and only does something with it on the second write to file (the change). But sometimes it jumps the gun, and converts to PDF immediately, creating a single blank page from the 0 kb file with nothing in it.
I've tried everything I can think of (or rather, I can find googling) but have failed to reliably fix this. Any ideas?
Here is my script code at present:
$folder = 'c:\Distill'
$filter = '*.ps'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;}
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
cd "C:\Program Files\GPLGS\" # changes to Ghostscript folder
.\ps2pdf "C:\Distill\Process.ps" "C:\PDFs\Process.pdf" # runs PS to PDF conversion
del "C:\Distill\Process.ps" # deletes no longer needed PostScript file
invoke-item "C:\PDFs\Process.pdf" # loads PDF file into default PDF reader
}
Any thoughts? Whilst I'm an IT Pro, I must admit I'm pretty much a novice when it comes to scripting, certainly PowerShell, so sorry in advance if I'm slow on the uptake at all :-)
Hope someone can help - many thanks in advance
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.