Skip to Main Content
  • Questions
  • UTL_FILE.FGETATTR can not find an existing file

Breadcrumb

Question and Answer

Connor McDonald

Thanks for the question, M.

Asked: February 24, 2024 - 6:49 am UTC

Last updated: February 26, 2024 - 5:47 am UTC

Version: 19C

Viewed 1000+ times

You Asked

I created a text file on oracle database server.
The name of the file is 'TestFile' and it is located in C:\TestFolder\TestFile.txt .
All C drive and 'TestFolder' folder and 'TestFile.txt' file have full control permission for everyone OS users.

I create a directory:
Create directory CheckFileExist as 'C:\TestFolder';

I grant read and write permissions on CheckFileExist directory to SYS oracle user:
Grant read, write on directory CheckFileExist to SYS;

I wrote a query so that Oracle can find the 'TestFile.txt' file or not:

Declare
V_File_Exists Boolean;
V_File_Length Number;
V_File_Size Number;
Directory_Name Nvarchar2(255):='CheckFileExist';
Begin

UTL_FILE.FGETATTR (Directory_Name, 'TestFile', V_File_Exists, V_File_Length, V_File_Size);

If V_File_Exists
Then DBMS_OUTPUT.PUT_LINE('File exists');
Else DBMS_OUTPUT.PUT_LINE('File does not exist');
End if;
End;

When I execute the query, the result is that File does not exist. What is the problem?

and Connor said...

You just need some care with the case and your file name


SQL> Create or replace directory CheckFileExist as 'C:\tmp';

Directory created.

SQL>
SQL> host ( echo Hello > c:\tmp\TestFile.txt )

SQL>
SQL> set serverout on
SQL> Declare
  2    V_File_Exists Boolean;
  3    V_File_Length Number;
  4    V_File_Size Number;
  5    Directory_Name Nvarchar2(255):='CheckFileExist';
  6  Begin
  7    UTL_FILE.FGETATTR (Directory_Name, 'TestFile', V_File_Exists, V_File_Length, V_File_Size);
  8    If V_File_Exists
  9      Then DBMS_OUTPUT.PUT_LINE('File exists');
 10      Else DBMS_OUTPUT.PUT_LINE('File does not exist');
 11    End if;
 12  End;
 13  /
File does not exist

PL/SQL procedure successfully completed.

SQL>
SQL> set serverout on
SQL> Declare
  2    V_File_Exists Boolean;
  3    V_File_Length Number;
  4    V_File_Size Number;
  5    Directory_Name Nvarchar2(255):='CheckFileExist';
  6  Begin
  7    UTL_FILE.FGETATTR (Directory_Name, 'TestFile.txt', V_File_Exists, V_File_Length, V_File_Size);
  8    If V_File_Exists
  9      Then DBMS_OUTPUT.PUT_LINE('File exists');
 10      Else DBMS_OUTPUT.PUT_LINE('File does not exist');
 11    End if;
 12  End;
 13  /
File does not exist

PL/SQL procedure successfully completed.

SQL>
SQL> set serverout on
SQL> Declare
  2    V_File_Exists Boolean;
  3    V_File_Length Number;
  4    V_File_Size Number;
  5    Directory_Name Nvarchar2(255):='CHECKFILEEXIST';
  6  Begin
  7    UTL_FILE.FGETATTR (Directory_Name, 'TestFile.txt', V_File_Exists, V_File_Length, V_File_Size);
  8    If V_File_Exists
  9      Then DBMS_OUTPUT.PUT_LINE('File exists');
 10      Else DBMS_OUTPUT.PUT_LINE('File does not exist');
 11    End if;
 12  End;
 13  /
File exists

PL/SQL procedure successfully completed.

SQL>
SQL>


Rating

  (1 rating)

Comments

A reader, February 26, 2024 - 5:47 am UTC

Thank you very much for your complete answer
Connor McDonald
February 26, 2024 - 5:47 am UTC

glad to help

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library