- SPLessons

PLSQL function to check If file exists in a directory

Home > > Tutorial
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

PLSQL function to check If file exists in a directory

PLSQL function to check If file exists in a directory

 

Description:

If you get a chance to work with PLSQL functions and file handling in your work then this tutorial will definitely help full. Here we will see with PLSQL function to check If file exists in a directory.   Step 1: In our previous tutorial we have seen how to CREATE DIRECTORY statement to create directory object in oracle data base. Step 2: I assume that you have already created directory object if you do not create directory object please check the previous tutorial Export oracle sql output to excel format Step 3: I have created procedure named as checkFielExistOrNot with argument v_dir, v_file. v_dir is directory object which we have create in the previous tutorial and  v_file is file name. Step 4: I have created three more variables in procedure named as lb_file_exist is checking flag, and  ln_size is for store the file size and ln_block_size is for block size of file. Step 5: Here we used UTL_FILE package function sys.utl_file.fgetattr. We will pass 5 arguments  
  1. v_dir - Directory Object -- input parameter
  2. v_file - File name -- input parameter
  3. lb_file_exist -- output parameter -- fgetattr function will return values will be bind to this parameter.
  4. ln_size -- output parameter-- fgetattr function will return values will be bind to this parameter.
  5. ln_block_size -- output parameter -- fgetattr function will return values will be bind to this parameter.
Step 6: sys.utl_file.fgetattr function will check the file exist in file systems and return the lb_file_exist value true or false. Step 7: Then our if condition will check the return value if it true then prints true else prints false.     [sql] declare PROCEDURE checkFileExistOrNot(v_dir IN VARCHAR2, v_file in VARCHAR2) IS lb_file_exist boolean; ln_size number; ln_block_size number; BEGIN sys.utl_file.fgetattr(v_dir,v_file,lb_file_exist,ln_size,ln_block_size); if lb_file_exist then dbms_output.put_line('true'); else dbms_output.put_line('false'); end if; END; begin checkFileExistOrNot('FINAIDREPORTS','splessons.xls'); end; / [/sql]