We help IT Professionals succeed at work.
Get Started

PowerShell script to carry out action on file generated in a specific folder (works, but 'jumping the gun' sometimes!)

bluemercury
bluemercury asked
on
556 Views
Last Modified: 2016-04-12
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
}

Open in new window


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
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2014
Commented:
This problem has been solved!
Unlock 1 Answer and 16 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE