Skip to Main Content

SQL & PL/SQL

Announcement

Testing banner

Check if directory exists

Yossi NixonAug 28 2014 — edited Aug 31 2014

By the following code I am checking an existance of a file in the database operating system:

DROP DIRECTORY mydir;

DEFINE my_os_directory='/tmp';

CREATE OR REPLACE DIRECTORY mydir AS '&&my_os_directory';

DECLARE
   ex      BOOLEAN := TRUE;
   blksz   NUMBER := 0;
   fl      NUMBER := 0;
BEGIN
   -- check if file exists
   UTL_FILE.fgetattr ('MYDIR'
                     ,'1.sql'
                     ,ex
                     ,fl
                     ,blksz);

   -- if directory does not exists - abort
   IF (ex <> TRUE)
   THEN
      raise_application_error (-20031, 'file 1.sql does not exists in &&my_os_directory');
   END IF;
END;
/

It is working fine

At the same way I am trying to check an existance of a directory:

DROP DIRECTORY mydir;

DEFINE my_os_directory='/tmp';

CREATE OR REPLACE DIRECTORY mydir AS '&&my_os_directory';

DECLARE
   ex      BOOLEAN := TRUE;
   blksz   NUMBER := 0;
   fl      NUMBER := 0;
BEGIN
   -- check if directory exists
   UTL_FILE.fgetattr ('MYDIR'
                     ,'.'
                     ,ex
                     ,fl
                     ,blksz);

   -- if directory does not exists - abort
   IF (ex <> TRUE)
   THEN
      raise_application_error (-20031, 'Directory &&my_os_directory does not exists');
   END IF;
END;
/

It is not working

any idea ?

This post has been answered by Zohar Elkayam on Aug 28 2014
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 28 2014
Added on Aug 28 2014
10 comments
9,948 views