$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
}
The file 'PROCESS.PS' was Created at 03/06/2016 21:00:54
The file 'PROCESS.PS' was Changed at 03/06/2016 21:01:01
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:02:40
The file 'PROCESS.PS' was Created at 03/06/2016 21:02:40
The file 'PROCESS.PS' was Changed at 03/06/2016 21:02:47
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:03:11
The file 'PROCESS.PS' was Created at 03/06/2016 21:03:11
The file 'PROCESS.PS' was Changed at 03/06/2016 21:03:18
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:13:27
The file 'PROCESS.PS' was Created at 03/06/2016 21:13:27
The file 'PROCESS.PS' was Changed at 03/06/2016 21:13:35
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:13:43
The file 'PROCESS.PS' was Created at 03/06/2016 21:13:43
The file 'PROCESS.PS' was Changed at 03/06/2016 21:13:44
The file 'PROCESS.PS' was Changed at 03/06/2016 21:13:51
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:14:55
The file 'PROCESS.PS' was Created at 03/06/2016 21:14:55
The file 'PROCESS.PS' was Changed at 03/06/2016 21:15:03
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:15:16
The file 'PROCESS.PS' was Created at 03/06/2016 21:15:16
The file 'PROCESS.PS' was Changed at 03/06/2016 21:15:17
The file 'PROCESS.PS' was Changed at 03/06/2016 21:15:24
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:15:31
The file 'PROCESS.PS' was Created at 03/06/2016 21:15:31
The file 'PROCESS.PS' was Changed at 03/06/2016 21:15:39
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:15:45
The file 'PROCESS.PS' was Created at 03/06/2016 21:15:45
The file 'PROCESS.PS' was Changed at 03/06/2016 21:15:53
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:16:08
The file 'PROCESS.PS' was Created at 03/06/2016 21:16:08
The file 'PROCESS.PS' was Changed at 03/06/2016 21:16:16
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:16:24
The file 'PROCESS.PS' was Created at 03/06/2016 21:16:24
The file 'PROCESS.PS' was Changed at 03/06/2016 21:16:32
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:16:52
The file 'PROCESS.PS' was Created at 03/06/2016 21:16:52
The file 'PROCESS.PS' was Changed at 03/06/2016 21:17:00
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:17:04
The file 'PROCESS.PS' was Created at 03/06/2016 21:17:04
The file 'PROCESS.PS' was Changed at 03/06/2016 21:17:12
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:17:41
The file 'PROCESS.PS' was Created at 03/06/2016 21:17:41
The file 'PROCESS.PS' was Changed at 03/06/2016 21:17:49
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:18:22
The file 'PROCESS.PS' was Created at 03/06/2016 21:18:22
The file 'PROCESS.PS' was Changed at 03/06/2016 21:18:30
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:18:35
The file 'PROCESS.PS' was Created at 03/06/2016 21:18:35
The file 'PROCESS.PS' was Changed at 03/06/2016 21:18:43
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:19:06
The file 'PROCESS.PS' was Created at 03/06/2016 21:19:06
The file 'PROCESS.PS' was Changed at 03/06/2016 21:19:13
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:23:23
The file 'PROCESS.PS' was Created at 03/06/2016 21:23:23
The file 'PROCESS.PS' was Changed at 03/06/2016 21:23:31
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:23:37
The file 'PROCESS.PS' was Created at 03/06/2016 21:23:37
The file 'PROCESS.PS' was Changed at 03/06/2016 21:23:44
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:49:21
The file 'PROCESS.PS' was Created at 03/06/2016 21:49:21
The file 'PROCESS.PS' was Changed at 03/06/2016 21:49:29
The file 'PROCESS.PS' was Deleted at 03/06/2016 21:49:45
The file 'PROCESS.PS' was Created at 03/06/2016 21:49:45
The file 'PROCESS.PS' was Changed at 03/06/2016 21:49:53
Register-ObjectEvent $fsw Changed -SourceIdentifier FileChanged -Action {
Write-Host "$(Get-Date) :: Change detected. Action started"
Write-Host "$(Get-Date) :: Sleeping for 10 seconds"
Start-Sleep -Seconds 10
cd "C:\Program Files\GPLGS\" # changes to Ghostscript folder
If ( Test-Path "C:\Distill\Process.ps" )
{
If ( (Get-Item "C:\Distill\Process.ps").length -gt 1000 )
{
Write-Host "$(Get-Date) :: Converting file to .PDF"
.\ps2pdf "C:\Distill\Process.ps" "C:\PDFs\Process.pdf" # runs PS to PDF conversion
Write-Host "$(Get-Date) :: Sleeping another 5 seconds"
Start-Sleep -Seconds 5
Write-Host "$(Get-Date) :: Deleting source file"
del "C:\Distill\Process.ps" # deletes no longer needed PostScript file
Invoke-Item "C:\PDFs\Process.pdf" # loads PDF file into default PDF reader
}
Else
{
Write-Host "$(Get-Date) :: File too small. Stopping action."
break
}
}
Write-Host "$(Get-Date) :: Source file not found"
Write-Host "$(Get-Date) :: Action exiting"
}
What happens when the ghostscript tries to run against a file that isn't there? I'm wondering if in the case of a double-trigger (or some other collision of circumstances) the file is getting deleted by one event, but another event has already launched another action, and when it gets to the conversion the file is gone.From what I've seen (and apologies, I've probably not explained it very well) I don't think this is happening. When it jumps the gun, it is clear that the file is there and not deleted at that point - but it is an empty file. So it registers in Windows Explorer as a 0kb file called Process.ps. I presume this is the process that WordStar goes through when creating the Process.ps file:
If ( (Get-Item "C:\Distill\Process.ps").length -gt 1000 )
define that the Process.ps file has to be certain data size in order to run? I tried an If statement nested in an If statement like this before, but with my lacking knowledge didn't know about the '.length' parameter. If it is what I think it is, that could be the absolute answer.If what you say is true about .PDF and Adobe Reader, then just removing the Invoke-Item command should avoid the issue.Agreed, although this is an important aspect to have running. My co-workers need a solution that brings up the PDF once done - if it relied on them going to a folder and manually opening after a certain time period, I can imagine all sorts of confusion and support calls (they are not wonderfully tech savvy). And they would have a point that it wouldn't be as efficient as it loading up as a PDF for them to physically print (or otherwise) automatically.
I'm still not 100% convinced that the issue as I described wouldn't happen with the original code. Just for my own curiosity, what does happen when you try to run the ghostscript against a non-existent file? In any case, what I posted should avoid that scenario.No probs - I didn't quite interpret your request properly before, think I follow now. What I should have mentioned, in all my testings of the script, is I have commented out any deletion commands / events previously, and still the problem with the 'jumping the gun' has happened. Now I understand your request, will try this shortly to quench any curiosity!!
Yes, that line does check the size of the file (in bytes). You could change the size as you see fit.That's exactly what I hoped you'd say. When the error happens, the initial PostScript file that is blank is always 0kb in size. So having something that (rounded) is looking for a 1kb file or higher is perfect. Typically, these PS files are not smaller than 5kb in size. So this is the golden bit of code I've been searching for, and it looks to be working in the PowerShell script you kindly wrote for me. I'm going to implement this into a nested 'IF' statement in my original script, and see if I can make it work. Was already dropping the sleep command, but saw the value in you putting that in for testing, as it showed the mechanics of the script running with pauses.
And feel free to confirm (or refute) that the Start-Sleep is working as expected. Once we get something working, the sleep command can probably be removed.
You could insert the following at line 6, pushing the rest down.
Open in new window