Press "Enter" to skip to content

How to Check If File Exists in PL/SQL?

The following is a stored function example to check if a file exists in PL/SQL.

PL/SQL Function Example To Check If a File Exists

The below function takes two parameters, one is for Oracle directory object name and the second one is the file name and returns the Boolean type. It will return true if a file exists else returns false.

CREATE OR REPLACE FUNCTION is_file_exist (p_directory IN VARCHAR2,
p_filename IN VARCHAR2)
RETURN BOOLEAN
AS
n_length NUMBER;
n_block_size NUMBER;
b_exist BOOLEAN := FALSE;
BEGIN
UTL_FILE.fgetattr (p_directory,
p_filename,
b_exist,
n_length,
n_block_size);

RETURN b_exist;
END is_file_exist;

Test

SET SERVEROUTPUT ON;

BEGIN
IF is_file_exist ('CSV_DIR', 'emp.dat')
THEN
DBMS_OUTPUT.put_line ('File exists.');
ELSE
DBMS_OUTPUT.put_line ('File not exists.');
END IF;
END;
/

Output

File exists.
PL/SQL procedure successfully completed.

See also:

Vinish Kapoor

Hi, I am a full stack developer and writing about development. I document everything I learn and help thousands of people. foxinfotech.in is created, written, and maintained by me; it is built on WordPress, and hosted by Bluehost. Connect with me on Facebook, Twitter, GitHub, and get notifications for new posts.

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *