Checking for the Existence of a File

To check for the existence of a file, you use the QUERY method of the Stream class. The following ISTHERE.CMD program accepts a file name as a command line argument and checks for the existence of that file.

/* ISTHERE.CMD - test for the existence of a file        */
parse arg fid                    /* Get the file name    */           /* Create stream object */
if qfile~query("exists")="" then /* Check for existence  */
   say fid "does not exist."
   say fid "exists."

In the example, a stream object is created for the file even though it might not exist. This is acceptable because the file is not opened when the stream object is created.

The QUERY method accepts one argument. To check for the existence of a file, you specify the string "exists" as previously shown. If the file exists, QUERY returns the full-path specification of the stream object. Otherwise, QUERY returns a null string.