To check for the existence of a file, you use the QUERY method of the Stream class. The following isthere&per;cmd program accepts a file name as a command line argument and checks for the existence of that file.
|/* isthere&per;cmd - test for the existence of a file */|
|parse arg fid /* Get the file name */|
|qfile=.stream~new(fid) /* 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.