6

trying to check whether the file I want to read exists or not.

eSS92
  • 241
  • 1
  • 3
  • 10

3 Answers3

12

Here are another approaches:

  1. Using BFILE and fileexists function of dbms_lob package:

    create or replace function FileExists(
       p_DirName in varchar2,     -- schema object name
       p_FileName in varchar2
     ) return number
    is
      l_file_loc bfile;
    begin
      l_file_loc := bfilename(upper(p_DirName), p_FileName);
      return dbms_lob.fileexists(l_file_loc);  -- 1 exists; 0 - not exists
    end;
    
  2. Using fgetattr function of utl_file package:

    create or replace function FileExists(
      p_DirName in varchar2,     -- schema object name
      p_FileName in varchar2
    ) return number
    is
      l_fexists boolean;
      l_flen   number;
      l_bsize  number;
      l_res    number(1);
    begin
      l_res := 0;
      utl_file.fgetattr(upper(p_DirName), p_FileName, l_fexists, l_flen, l_bsize);
      if l_fexists
      then
        l_res := 1;
      end if;  
      return l_res;
    end;
    
Nick Krasnov
  • 26,886
  • 6
  • 61
  • 78
  • @ElShaikhaShaikha Try to grant `execute` privilege on `UTL_FILE` package to a user that going to use that package or to the `public`. – Nick Krasnov Dec 21 '12 at 09:07
  • Thanks. I did not have to grant execute on UTL_FILE to public. The first function worked perfectly. I edited it to fit my DB needs. Thank you again for your help. – eSS92 Dec 21 '12 at 09:23
3

Use UTL_FILE.FGETATTR function. This function is designed specifically for this purpose.

Syntax:

UTL_FILE.FGETATTR(
   location     IN VARCHAR2, 
   filename     IN VARCHAR2, 
   fexists      OUT BOOLEAN, 
   file_length  OUT NUMBER, 
   block_size   OUT BINARY_INTEGER);

Example:

DECLARE
  fexists      BOOLEAN;
  file_length  NUMBER;
  block_size   BINARY_INTEGER;
BEGIN

    UTL_FILE.FGETATTR('MY_ORA_DIRECTORY', 'my_file_name.csv', fexists, file_length, block_size);
    IF fexists THEN
       -- Do something
       -- ...
    END IF;

END IF;

Oracle documentation: https://docs.oracle.com/database/121/ARPLS/u_file.htm#ARPLS70915
One more useful link: https://www.foxinfotech.in/2018/09/how-to-check-if-file-exists-in-pl-sql.html

Andre
  • 31
  • 2
  • 1
    Hi, welcome to Stack Overflow. Would you mind adding an example of how you'd use `UTL_FILE.FGETATTR` within the answer itself, just in case the site you've linked to goes down? Also, if you haven't done so already, please [take the tour](https://stackoverflow.com/tour) and read ["How do I write a good answer?"](https://stackoverflow.com/help/how-to-answer) – Michael Dodd Mar 12 '19 at 11:15
  • Thank you, guys. I have corrected the answer. Hope, now, it looks better ) – Andre Mar 13 '19 at 12:29
2

Creating a function that checks if a file exists is fairly easy by just trying to open it and catching any exceptions (this example function taken from AskTom)

CREATE OR REPLACE FUNCTION file_exists(p_fname IN VARCHAR2) RETURN BOOLEAN
AS
  l_file UTL_FILE.FILE_TYPE;
BEGIN
  l_file := UTL_FILE.FOPEN(SUBSTR( p_fname, 1, instr(p_fname,'/',-1) ),
                           SUBSTR( p_fname, instr( p_fname, '/', -1)+1 ), 'r' );   
  UTL_FILE.FCLOSE( l_file );
  RETURN TRUE;
EXCEPTION
  WHEN UTL_FILE.INVALID_PATH      THEN RETURN FALSE;
  WHEN UTL_FILE.INVALID_OPERATION THEN RETURN FALSE;
END;
/

Then you can just use;

IF ( file_exists( 'MED_LIST_19_OCT_12.csv' ) )
...
Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • I get these errors: Error(7,1): PL/SQL: Statement ignored Error(7,1): PLS-00201: identifier 'UTL_FILE' must be declared – eSS92 Dec 21 '12 at 08:00
  • @ElShaikhaShaikha To keep things secure (ie you don't want anyone with SQL access to read/write everywhere in your file system), you'll need to enable it explicitly in init.ora for the directories you're interested in, see for example [this link](http://utplsql.sourceforge.net/Doc/admin.html) how to do that. – Joachim Isaksson Dec 21 '12 at 08:06