Skip to main content
Participating Frequently
August 1, 2012
Question

Check if File Exists

  • August 1, 2012
  • 3 replies
  • 3222 views

I am working on a Postscript program that uses EPS files that exist on our printers.  Right now, if the EPS does not exist the printer throws an error.  I would like to see the form print even if the EPS does not exist.

The EPS that I am checking is a logo that changes depending on internal conditions.  We have about 300 EPS's on the printer, but some times one is missed. 

The EPS is always in the format ####.EPS (4105.EPS for example).

I am calling the eps As follows:

%!
/ExtraLogo
{
    420 711 translate
  (6661.EPS) run
  -420 -711 translate
} def
%%....  OTHER STUFF
ExtraLogo
showpage

I would like to include an If statement such as if ####.EPS exists, print, else do nothing..

Is there any way that this can be done?

Thanks

Eric

This topic has been closed for replies.

3 replies

Participating Frequently
August 21, 2012

Hi, Eric -

I'd take a different approach to this problem; rather than test for the existence of the file, I'd wrap the "run" call in a "stopped" invocation:

     /ExtraLogo

     {

         /mysaveobj save def                      % Do our bookkeeping

          mark                                              % Push a mark on the stack

          {  (6661.EPS) run } stopped          % Try to execute the file

          { (File failed) = } if                         % If it fails, emit a simple error message

          cleartomark                                   % Clean up the stack

          mysaveobj  restore                         % Reclaim VM

     } def

Note that the code in the "if" procedure body can be anything you wish, including an alternative version of the logo.

In the interest of robustness, you should arguably always execute an external file in a stopped context. I presume you are also doing all the other good practices, such as doing a save and restore around the external call.

The nice thing about this approach is that it will catch anything that goes wrong with the execution of the file: permission problems, errors in the PS code, etc.

If you're interested, there's an article on using EPS files in handwritten PS code in the November 2004 issue (#36) of the Acumen Journal (www.acumentraining.com/acumenjournal.html).

- John

-------

John Deubert

Acumen Training

PostScript & PDF consulting and training

www.acumentraining.com

Mr__Horton
Inspiring
August 8, 2012

How about taking Helge's suggestion and writing something like this:

(queried.eps) dup status true {run  %runs queried.eps file if it exists

}

{pop  %pops the string (queried.eps) off the operand stack and then goes on to the next part of your PS program

}ifelse

Participating Frequently
August 9, 2012

I will try that out today.

Participating Frequently
August 13, 2012

I have tried that out.  and If I do this:

/BrokerLogo
{
(VALIDLOGOFILE.EPS) dup status true {

       (VALIDLOGOFILE.EPS) run
}{pop}ifelse

} def

The logo prints.

If I try a file name that does not exist, I still tet undefunedfilename.

The Operandstack lists as follows on the error page:

===top of stack===

(INVALIDLOGOFILE.EPS)

false

(INVALIDLOGOFILE.EPS)

I hope to get more time to work on this today. 

Participating Frequently
August 4, 2012

Look into the PLRM for the status operator. If the file exists, it returns - among a couple of properties - true and false

otherwise (see the PLRM for details)

Helge

Participating Frequently
August 7, 2012

I think I am making progress.  I was able to handle the missing file names with a custom undefinedfilename handler.

For example

errordict begin

/*undefinedfilename /undefinedfilename load def

/undefinedfilename { %def

  (DEFAULT.EPS) run

} bind def

end

This seems to work fine for me although this will not be helpfull if any other eps is missing, as any missing eps will get the default one printed.  (in my case the report would be un usable if any other eps was missing.

I would like to build better error handling in to my report so I will keep playing with this, although I think I am on the right track.