Check if file exists

Question : Check if file exists

Hello,

I thought this would be easy but am at a loss! We’ve put together an images database using Oracle 8i and Forms 6i (client-server). We have a table of images, containing filenames that reference an NT drive (called “DIS”) that contains the actual images (JPGs, TIFs, etc).

We have created a form which allows a user to “import” their own images into the database. They type in the path/filename of their image (e.g. from their C:) and press a button which creates a batch script which copies the file to the DIS and inserts rows into the images table referencing the new image. The batch script is run using the HOST command.

Our problem is, if for some reason the copy fails, HOST does not tell us the copy failed. The user can enter as many filenames as they wish (they will often be pulling in numerous photos from a digital camera, for e.g.) and they are copied in bulk by the batch script.

Now, we simply want to create a function on the form that checks that the file now exists on the DIS as expected. If the function returns FALSE, we can give an appropriate error message to the user.

We were going to use UTL_FILE.FOPEN to test whether the file exists, but it fails with INVALID_PATH. Here is some test code we’re using (this is run in a WHEN-BUTTON-PRESSED trigger in the form, which is being run on a developer PC):

DECLARE
v_file utl_file.file_type;
BEGIN
v_file := UTL_FILE.FOPEN(‘\GeminiDISPhotos’,’myphoto.jpg’,’r’);
UTL_FILE.FCLOSE(v_file);
END;

DECLARE
*
ERROR at line 1:
ORA-20000: utl_file.invalid_path
ORA-06512: at line 12


 

Solution: Check if file exists

An altenative way is to use Forms:

You can create a temporary file in the filesystem using HOST built-in:
dir “filename” /b > temp.out
and using TEXT_IO you can read the file (temp.out) and if it is null then file is not there and if the file exists you will find an entry in the file.

You can check whether the file exists in a particular directory or not using HOST built-in.

Example:

To test TESTFILENAME.txt exists in dir C.

WHEN-BUTTON-PRESSED trigger:

HOST(‘dir C:TESTFILENAME.txt’);
IF NOT FORM_SUCCESS THEN
Message(‘File not found’);
Raise form_trigger_failure;
ELSE
Message(‘File Exists’);
END IF;

If the file is on the server you have run forms on the server or open access to the server files. This is not so dangerous, because in the case of UTL_FILE you also have to open access to the files. Of course the access can be done only using oracle procedures.