UTL_FILE.FGETATTR
I need to read a file on the DIR ( UTL_FILE_DIR = location ) , and make sure that exists, so :
1 - Situation "A"a given file name :
SQL> set serveroutput on
SQL> DECLARE
2
3 ex BOOLEAN;
4 flen NUMBER;
5 bsize NUMBER;
6
7 BEGIN
8 utl_file.fgetattr('FLATFILES', 'test.txt', ex, flen, bsize);
9
10 IF ex THEN
11 dbms_output.put_line('File Exists');
12 ELSE
13 dbms_output.put_line('File Does Not Exist');
14 END IF;
15 dbms_output.put_line('File Length: ' || TO_CHAR(flen));
16 dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
17 END fgetattr;
18 /
File Exists
File Length: 314
Block Size: 0
OK OK OK OK , no problem here ...
2 - Situation "B"No names ...
SQL> set serveroutput on
SQL>
SQL> DECLARE
2
3 ex BOOLEAN;
4 flen NUMBER;
5 bsize NUMBER;
6
7 BEGIN
8 utl_file.fgetattr('FLATFILES', '*.txt', ex, flen, bsize);
9
10 IF ex THEN
11 dbms_output.put_line('File Exists');
12 ELSE
13 dbms_output.put_line('File Does Not Exist');
14 END IF;
15 dbms_output.put_line('File Length: ' || TO_CHAR(flen));
16 dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
17 END fgetattr;
18 /
File Does Not Exist
File Length: 0
Block Size: 0
Ups... can't search by extension ...
Please any Ideias ...
UTL_FILE can only work with named files. It is not intended to work with OS directories. This is a good situation to use a Java Stored Procedure. The inestimable Mr Kyte has an example on his AskTom web site.
Cheers, APC
Similar Messages
-
Dbms_lob.fileopen failed though utl_file.fgetattr works fine
Hi ,
I am using Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Prod.
I am working on loading MS .doc into Oracle.
I have created dir using following code:
BEGIN
EXECUTE IMMEDIATE
'CREATE OR REPLACE DIRECTORY TEST_SN1 AS'||
'''\\inecg-sdc11\XYZ_ABC2\DT-AUTOMATION\DUMMY_12LNEW''';
END;
I have verified the existence of file(Status.doc) using utl_file.fgetattr using the following code:
DECLARE
ex BOOLEAN;
flen NUMBER;
bsize NUMBER;
BEGIN
utl_file.fgetattr('TEST_SN1', 'Status.doc', ex, flen, bsize);
IF ex THEN
insert into test_msg values('File Exists');
ELSE
insert into test_msg values('File Does Not Exist');
END IF;
insert into test_msg values('File Length: ' || TO_CHAR(flen));
insert into test_msg values('Block Size: ' || TO_CHAR(bsize));
commit;
END;
the code succuessfully returns the msg 'File exists' along with the file length .
I have also tried opening the file using utl_file and dbmc_lob package+
SQL> DECLARE
2 vInHandle utl_file.file_type;
3 vNewLine VARCHAR2(250);
4 BEGIN
5 vInHandle:= utl_file.fopen('TEST_SN1', 'Status.doc', 'R');
6 utl_file.fclose(vInHandle);
7 END;
8 /
PL/SQL procedure successfully completed.
But the following code still produces error ORA-22288:+
This is what the output was:
SQL> declare
2 l_bfile bfile;
3 begin
4 l_bfile := bfilename( 'TEST_SN1', 'Status.doc' );
5 dbms_lob.fileopen( l_bfile );
6 dbms_lob.fileclose( l_bfile );
7 end;
8 /
declare
ERROR at line 1:
ORA-22288: file or LOB operation FILEOPEN failed
ORA-06512: at "SYS.DBMS_LOB", line 523
ORA-06512: at line 5
Even though the file exists and Oracle is able to identify the file, I cant figure out what is
stopping my code to open that file.
Please help me to figure out the problem.
For more undertstanding ,You can also refer the my conversation with Tom Kyte at following link:
http://asktom.oracle.com/pls/apex/f?p=100:11:0::::P11_QUESTION_ID:636223754581#2815252100346024210
Thanks a million tons!
Regards
Saurabh Nolakha,
BangaloreHi,
Thanks for your quick response.
The file Status.doc which I am trying to access is in remote server at path *\\inecg-sdc11\XYZ_ABC2\DT-AUTOMATION\DUMMY_12LNEW* .Though the path is not mapped to the WINDOWS system where Oracle is installed but I am able to access the file(open and read) from that system.
Do you mean that I need to map the network drive to that system?*
if yes, then why UTL_FILE is working perfectly?*
Also, in my requirement, theres a high possibility that the path(of remote server) may change in future(if mapped).So ,is there any alternative for that?*
Please provide your useful suggestions
Regards
Saurabh Nolakha -
UTL_FILE.FGETATTR - Different in 10g?
Hello,
We have implemented Oracle E-Business Suite in 9i environment and it is up and running for 3+ years. We are migrating to a 10g environment and currently performing an end-to-end testing.
We have a custom PL/SQL program that works perfectly in 9i environment but not so in 10g. The point of failure is UTL_FILE.FGETATTR. We use this procedure to derive the filesize and pass it to another program. In 10g, the filesize returned is NULL and so the program fails.
It would be grateful if someone let me know of any changes in the procedure between 9i and 10g.
Regards,
BalaThanks for the reply.
But I found the following link that specifically answers to my question.
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:3481564363142 -
Utl_file.fgetattr not working in Oracle Reports
Hi there,
I try to use the utl_file.fgetattr function in an Oracle Report program unit as below, but it returns that the file does not exist. However executing it in TOAD or SQL Developer it return that the file exists.
utl_file.fgetattr('directory', 'filename.ext', v_exists, v_length, v_blocksize);
IF v_exists THEN
srw.message(100, 'The file exists.');
ELSE
srw.message(100, 'The file does not exist.');
END IF;
What could be the problem?
Thanks in advance!
Edited by: user13428604 on 2012.02.13. 9:26You have to use utl_file inside the database. So, move your code to a stored procedure and call that procedure from Reports.
-
Little help needed on utl_file
Hi
I am trying to write a small stored procedure for recording some information to a text file, and I need a little help. However, beforehand, let me give you what I have done:
procedure create_record (order_id IN VARCHAR2) IS
l_dir VARCHAR2(10) 'c:/orders';
l_filename VARCHAR2(25) := 'orderlist';
l_datetime DATE := sysdate;
l_output utl_file.file_type;
begin
l_output := utl_file.fopen(l_dir, l_filename, 'w');
if !utl_file.fopen(l_output) THEN
utl_file.put_line(l_output, l_datetime || order_id);
utl_file.fclose(l_output);
else
utl_file.fclose(l_output);
l_output := utl_file.fopen(l_dir, l_filename, 'a');
end if;
end;
Now for questions, firstly, I am not entirely sure that the if statement for checking to see if an existing text file exists is the correct way, and would welcome some help on this, and correction.
Secondly, I need to add a second statement to the if, which if the file exists and the date on that file is the same as the system date, then to open and append some information to it, else create a new file.
Unfortunately, for me, I am not sure how to do this.
Can anyone show me how this can be done?
ThanksHope you can read my coding correctly.
ps: not tested
DECLARE
myFileExist BOOLEAN;
myFileLength NUMBER;
myFileBlockSize BINARY_INTEGER;
myText VARCHAR2(1000):= NULL;
myInstr NUMBER;
l_dir VARCHAR2(10) 'c:/orders';
l_filename VARCHAR2(25) := 'orderlist';
l_datetime DATE := trunc(sysdate);
l_output utl_file.file_type;
BEGIN
utl_file.fgetattr(l_dir, l_filename, myFileExist, myFileLength, myFileBlockSize);
if not myFileExist then -- file not exist
<ul>l_output := utl_file.fopen(l_dir, l_filename, 'w');
utl_file.put_line(l_output, l_datetime || order_id);</ul>
else --file exist
<ul>
l_output := utl_file.fopen(l_dir, l_filename, 'r');
LOOP
<ul>
--------------------------------------------------------------------start loop
UTL_FILE.GET_LINE(l_output,myText) ;
SELECT instr(UPPER(myText),UPPER(trunc(SYSDATE))) into myInstr FROM dual;
IF myText IS NULL THEN
<ul>
EXIT;
</ul>
END IF;
if myInstr > 0 then
<ul>
utl_file.fclose(l_output);
l_output := utl_file.fopen(l_dir, l_filename, 'a');
</ul>
else
<ul>
utl_file.fclose(l_output);
l_filename:='newfilename';
l_output := utl_file.fopen(l_dir, l_filename, 'w');
</ul>
end if;
--------------------------------------------------------------------end loop
</ul>
END loop;
utl_file.put_line(l_output, l_datetime || order_id);
</ul>
end if;
utl_file.fclose(l_output);
END;
Edited by: Lie Ching Te on 12-Feb-2010 12:34 PM
Edited by: Lie Ching Te on 12-Feb-2010 1:01 PM -
Change directory path dynamically using UTL_FILE
I have a directory TEST_DIR which points to /test/files/
CREATE OR REPLACE PROCEDURE file_exist (v_country in varchar2) is
v_check_file_exist BOOLEAN;
v_dir VARCHAR2 (256) := 'TEST_DIR';
begin
UTL_FILE.fgetattr (V_DIR || V_COUNTRY,'file123' ||'.txt', v_check_file_exist, v_a,v_b);
IF NOT v_check_file_exist THEN
DBMS_OUTPUT.put_line (TO_CHAR(from_date,'YYYYMMDD')||'.rds');
END IF;
END LOOP;
I would like to change the directory path based on the country which is passed as input to the procedure
exec file_exist('IND'), so in this case I want the procedure to look for file 'file123.txt' under path /test/files/IND.
But this is not working..
But when I change the directory to
create or replace directory TESTDIR as '/test/files/IND' and then change my UTL_FILE.fgetattr (V_DIR,'file123' ||'.txt', v_check_file_exist, v_a,v_b)(removing || v_country, this time it works.
am trying this as I have many country folders under the path like
/test/files/IND
/test/files/USA
/test/files/AFR
and want to change the direcotry path dynamically according to input given.
Any suggestionsMake sure that the directories you are trying to write to have been set up for UTL_FILE.
Refer to UTL_FILE_DIR parameter or CREATE DIRECTORY privilege depending on what DB version you are on.
HTH
--Johnnie -
Utl_file how to start reading from a certain position?
Hi all,
i'm currently using utl_file to read a text file and import to db. i have managed to do it. however i would like to avoid reading the first line as it is the header. i would also like to avoid the last line as it is the footer.
how do i use the function of get line to start reading at a certain position? i am also able to get the length but i do not know how to make it start reading from a certain position.
the length of the first line will always be 9 and length for the last line would be 51.
here's my code so far:
set serveroutput on
declare
vSFile utl_file.file_type;
vNewLine VARCHAR2(4000);
file_name VARCHAR2(200) := 'read.txt';
v_exist BOOLEAN;
v_length number;
blocksize NUMBER;
temp_value varchar2 (4000);
BEGIN
vSFile := utl_file.fopen('MYDIR', file_name,'r');
IF utl_file.is_open(vSFile) THEN
LOOP
BEGIN
utl_file.get_line(vSFile, vNewLine);
IF vNewLine IS NULL THEN
EXIT;
END IF;
EXCEPTION
WHEN NO_DATA_FOUND THEN
EXIT;
END;
END LOOP;
UTL_FILE.FGETATTR('MYDIR',file_name,v_exist,v_length,blocksize);
dbms_output.put_line(v_length);
COMMIT;
END IF;
utl_file.fclose(vSFile);
END read_demo;Hi,
utl_file has an fseek procedure for moving the file pointer around in a file, either absolute or relative number of bytes.
It's in the documentation, I'm surprised you didn't see it there when you looked before going to the hassle of asking us a question.
Andre -
Util_file.fgetattr 'file does not exist'
Oracle Database 11g Enterprise Edition Release 11.1.0.7.0 - Production
PL/SQL Release 11.1.0.7.0 - Production
CORE 11.1.0.7.0 Production
TNS for 32-bit Windows: Version 11.1.0.7.0 - Production
NLSRTL Version 11.1.0.7.0 - Production
The file exists and thesqldr uses this file. Here is my code.
set serveroutput on
DECLARE
v_dir VARCHAR2(200); -- Directory containing the data file
v_filename VARCHAR2(100); -- Data filename
v_file_exists boolean;
v_file_length number;
v_block_size number;
BEGIN
v_dir := '\\nrs2\WEBSITE\INCOMING\STARPUBS\';
v_filename := 'PC.ORDERS';
DBMS_OUTPUT.PUT_LINE(v_filename); --shows filename
DBMS_OUTPUT.PUT_LINE(v_dir); --shows directory
utl_file.fgetattr(v_dir, v_filename, v_file_exists, v_file_length ,v_block_size );
IF v_file_exists THEN
dbms_output.put_line('File Exists');
ELSE
dbms_output.put_line('File Does Not Exist');
END IF;
END;
Output:
SQL> @C:\STARPUBS\STARPUBS\dataformats\sql\fileOrderscheck.sql
PC.ORDERS
\\nrs2\WEBSITE\INCOMING\STARPUBS\
File Does Not Exist
PL/SQL procedure successfully completed.Create or replace the directory locally works:
create or replace directory USER_DIR as 'D:\wkdir';
grant read on directory USER_DIR to USER;
However when creating a directory to another network file on another server it says file doesn't exist
create or replace directory SYS_DIR as ''\\nrs2\WEBSITE\INCOMING\STARPUBS\';
grant read on directory SYS_DIR to USER;
The nrs2 server is used as a file server and doesn't have oracle client.
So my question is can the CREATE Directory statement manage access to another file system or is it limited to only local files?
If so, is there another way of accessing these files in the nrs2 server using UTL_FILE package?
Thanks
Mike. -
(9I R2) UTL_FILE.FREMOVE를 이용하여 OS FILE을 지우는 방법
제품 : PL/SQL
작성날짜 : 2003-01-20
(9I R2) UTL_FILE.FREMOVE를 이용하여 OS FILE을 지우는 방법
=========================================================
PURPOSE
UTL_FILE package을 사용하여 OS file을 지우는 sample을 만들어 본다.
UTL_FILE.FREMOVE는 Oracle 9i Release 2 이상에서 가능하며 이전 버젼에서는
제공되지 않는다.
Explanation
Oracle 9i Release 2 에서 UTL_FILE package에 다음과 같은 새로운 procedure가
추가되었다.
UTL_FILE.FCOPY(), UTL_FILE.FRENAME(), UTL_FILE.FREMOVE() , UTL_FILE.FGETATTR()
1) FCOPY ( Unix에서의 cp와 유사 ) procedure
: 모든 또는 일부분의 file을 copy한다.
2) FRENAME (Unix에서의 mv와 유사) procedure
: 존재하고 있는 file을 새로운 이름으로 바꾸며, 또한 이미 같은 file 이름이 존재한다면
overwrite할건지의 option이 존재한다.
3) FREMOVE (Unix에서의 rm와 유사) procedure
: 적당한 DIRECTORY OBJECT privilege 와 operating system
privilege가 있다면, DISK에 존재하는 file을 지운다.
4) FGETATTR procedure
:file의 특성을 읽고 그 값을 return한다.
UTL_FILE.FCOPY (location, filename, dest_dir,
dest_file, start_line, end_line);
UTL_FILE.FRENAME (location, filename, dest_dir, dest_file, overwrite);
UTL_FILE.FREMOVE (location, filename);
UTL_FILE.FGETATTR (location, filename, exists,
file_length, blocksize, create_date, mod_date);
Example
예제 1)
SQL> conn / as sysdba
Connected.
SQL> CREATE OR REPLACE DIRECTORY NEW_DIR as '/home/ora920/tool/jooyeon';
Directory created.
SQL> GRANT READ ON DIRECTORY NEW_DIR to scott;
Grant succeeded.
SQL> !ls new.txt
new.txt
SQL> BEGIN
UTL_FILE.fremove ('NEW_DIR','new.txt');
EXCEPTION
WHEN UTL_FILE.delete_failed THEN
dbms_output.put_line('Error while deleting the file');
END;
/ 2 3 4 5 6 7
PL/SQL procedure successfully completed.
예제 2)
SQL> connect sys/manager as sysdba
Connected.
SQL> create or replace directory public_access as '/home/ora920/tool';
Directory created.
SQL> grant read on directory public_access to public;
Grant succeeded.
SQL> conn scott/tiger
Connected.
SQL> create table tcopy01_out (line varchar2(500), i number);
Table created.
SQL>
create procedure tcopy01_p as
errbuf varchar2(50);
dir varchar2(512) := 'PUBLIC_ACCESS';
f1 utl_file.file_type;
type t_files is table of utl_file.file_type
index by binary_integer;
files t_files;
i number := 0;
ok boolean := TRUE;
pos number;
len number;
blk number;
procedure insertoutput (line varchar2) is
begin
insert into tcopy01_out values (line, i);
i := i+1;
end insertoutput;
begin
f1 := utl_file.fopen('PUBLIC_ACCESS', 'tcopy01.dat', 'w');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 1.');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 2.');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 3.');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 4.');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 5.');
utl_file.put_line(f1, 'Copy tcopy01.dat to tcopy01c.dat, line 6.');
utl_file.fclose(f1);
-- Full copy
begin
utl_file.fcopy('PUBLIC_ACCESS', 'tcopy01.dat','PUBLIC_ACCESS','tcopy01c.dat');
f1 := utl_file.fopen('PUBLIC_ACCESS', 'tcopy01c.dat', 'r');
utl_file.fclose(f1);
insertoutput('SUCCESS: Copied 1 file');
exception
when others then
errbuf := substr(sqlerrm, 1, 50);
insertoutput ('Unexpected exception raised: '||errbuf);
end;
-- Partial copy
begin
utl_file.fcopy('PUBLIC_ACCESS', 'tcopy01.dat','PUBLIC_ACCESS',
'tcopy01c2.dat',2,4);
utl_file.fgetattr('PUBLIC_ACCESS','tcopy01c2.dat',ok,len,blk);
insertoutput('SUCCESS: Copied file size='||len);
exception
when others then
errbuf := substr(sqlerrm, 1, 50);
insertoutput ('Unexpected exception raised: '||errbuf);
end;
begin
utl_file.fcopy('PUBLIC_ACCESS', 'tcopy01.dat','PUBLIC_ACCESS',
'tcopy01.dat');
utl_file.fgetattr('PUBLIC_ACCESS','tcopy01.dat',ok,len,blk);
insertoutput('SUCCESS: Copied file size='||len);
exception
when others then
errbuf := substr(sqlerrm, 1, 50);
insertoutput ('Unexpected exception raised: '||errbuf);
end;
-- Rename
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename01.dat', 'w');
utl_file.put_line(f1, 'File b, line 1.');
utl_file.fclose(f1);
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename012.dat', 'w');
utl_file.put_line(f1, 'File b, line 1.');
utl_file.fclose(f1);
-- Successful case
begin
utl_file.frename('PUBLIC_ACCESS','trename01.dat','PUBLIC_ACCESS','trename01c.dat');
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename01c.dat', 'r');
utl_file.fclose(f1);
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename01.dat', 'r');
exception
when utl_file.invalid_path then
insertoutput('SUCCESS: file renamed ');
when utl_file.rename_failed then
insertoutput('FAILURE: rename failed ');
end;
-- Successful case with overwrite flag
begin
utl_file.frename('PUBLIC_ACCESS','trename012.dat','PUBLIC_ACCESS','trename01c.dat',TRUE);
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename01c.dat', 'r');
utl_file.fclose(f1);
f1 := utl_file.fopen('PUBLIC_ACCESS', 'trename01.dat', 'r');
exception
when utl_file.invalid_path then
insertoutput('SUCCESS: file renamed ');
when utl_file.rename_failed then
insertoutput('FAILURE: rename failed ');
end;
commit;
exception
when others then
errbuf := substr(sqlerrm, 1, 50);
insertoutput ('Unexpected exception raised: '||errbuf);
commit;
end;
show errors
SQL> exec tcopy01_p;
ora920:/home/ora920/tool> ls -al
GU0h 40
drwxr-xr-x 5 ora920 dba 4096 1?y 20 10:19 ./
drwxr-x--- 31 ora920 dba 8192 1?y 20 13:05 ../
-rw-r--r-- 1 ora920 dba 0 1?y 20 10:19 tcopy01.dat
-rw-r--r-- 1 ora920 dba 252 1?y 20 10:19 tcopy01c.dat
-rw-r--r-- 1 ora920 dba 126 1?y 20 10:19 tcopy01c2.dat
-rw-r--r-- 1 ora920 dba 16 1?y 20 10:19 trename012.dat
-rw-r--r-- 1 ora920 dba 16 1?y 20 10:19 trename01c.dat
ora920:/home/ora920/tool> cat *.dat
Copy tcopy01.dat to tcopy01c.dat, line 1.
Copy tcopy01.dat to tcopy01c.dat, line 2.
Copy tcopy01.dat to tcopy01c.dat, line 3.
Copy tcopy01.dat to tcopy01c.dat, line 4.
Copy tcopy01.dat to tcopy01c.dat, line 5.
Copy tcopy01.dat to tcopy01c.dat, line 6.
Copy tcopy01.dat to tcopy01c.dat, line 2.
Copy tcopy01.dat to tcopy01c.dat, line 3.
Copy tcopy01.dat to tcopy01c.dat, line 4.
File b, line 1.
File b, line 1.
References
<Note:216450.1>
<Note:196948.1>
Korean Bulletin : 18488
PURPOSE
Explanation
Example
Reference Documents
------------------- -
Utl_file.open_file question to check if a file exists and is not empty
Hello,
I am trying to write a code where in 3 seperate files are created based on some parameter condition for the extract call.
Say the parameters are NULL, 'A' and 'B'.
When Null condition is passed to the extract call then it should create file A and B if records are found. If not it should create empty files.
It is possible that the parameter to the extract is just 'A' then it would create A with say non zero file and B with zero file.
Now when the extract is called with 'B' parameter and if the file A already exists and is not empty then I should not touch/overwrite it with empty records.
Is there any simple way that I can do this to check if a file exists and is non empty?
I am using
open_file
( gv_utl_file_path
,lv_file_name
,lv_file_handle
Please help.
Thanks@OP : No Need to open
SQL> declare
2 lb_file_exist boolean;
3 ln_size number;
4 ln_block_size number;
5 begin
6 sys.utl_file.fgetattr('TEST_DIR','a.txt',lb_file_exist,ln_size,ln_block_size);
7 if lb_file_exist then
8 dbms_output.put_line('a Exists');
9 dbms_output.put_line(to_char(ln_size));
10 else
11 dbms_output.put_line('a Not Exists');
12 end if;
13 sys.utl_file.fgetattr('TEST_DIR','b.txt',lb_file_exist,ln_size,ln_block_size);
14 if lb_file_exist then
15 dbms_output.put_line('b Exists');
16 dbms_output.put_line(to_char(ln_size));
17 else
18 dbms_output.put_line('b Not Exists');
19 end if;
20 sys.utl_file.fgetattr('TEST_DIR','c.txt',lb_file_exist,ln_size,ln_block_size);
21 if lb_file_exist then
22 dbms_output.put_line('c Exists');
23 dbms_output.put_line(to_char(ln_size));
24 else
25 dbms_output.put_line('c Not Exists');
26 end if;
27 end;
28 /
a Exists
0
b Exists
3
c Not Exists
PL/SQL procedure successfully completed.Edited by: jeneesh on Mar 30, 2012 1:21 AM -
Utl_fil - checking for file not found
Hi,
I am reading in a txt file whose name is specified in a parameter.
How do I trap the not found condition using utl_file in opening the txt file.
Thanx.....Maybe like this:
SQL> DECLARE
f UTL_FILE.file_type;
fexists BOOLEAN;
file_length NUMBER;
block_size BINARY_INTEGER;
BEGIN
UTL_FILE.fgetattr ('TEMP', 'file1.txt', fexists, file_length, block_size);
IF fexists
THEN
DBMS_OUTPUT.put_line ('File exists');
ELSE
DBMS_OUTPUT.put_line ('File does not exist');
END IF;
END;
File does not exist -
hey is there any way to check the filename from OS in pl/sql block. if we have a oracle directory named ABC reside physically on c:\backup. backup folder contain 4 different name file like PKMX1, PKMX2.dmp is there any UTL package which enable us to check if filename already there then remove old one create new. i have found UTL package which delete file from directory but how can i check either filename already there or not. plz check this
UTL_FILE package
FUNCTION FOPEN (location IN VARCHAR2,
filename IN VARCHAR2,
open_mode IN VARCHAR2)
RETURN UTL_FILE.FILE_TYPE;You should use FGETATTR procedure and it's exist parameter to know whether the file exists or not
http://download.oracle.com/docs/cd/B19306_01/appdev.102/b14258/u_file.htm
FGETATTR Procedure
This procedure reads and returns the attributes of a disk file.
Syntax
UTL_FILE.FGETATTR(
location IN VARCHAR2,
filename IN VARCHAR2,
exists OUT BOOLEAN,
file_length OUT NUMBER,
blocksize OUT NUMBER);- - - - - - - - - - - - - - - - - - - - -
Kamran Agayev A. (10g OCP)
http://kamranagayev.wordpress.com -
O/s commands from PL/SQL
hi all..
i want to execute operating system commands from PL/SQL..
particulary imp command....
i have used the f/wg approach but while i execute the imp command it does not responds me any output..
I have to kill the process from o/s.
prodn is my user
--AS SYS user
begin
dbms_java.grant_permission( 'PRODN',
'SYS:java.io.FilePermission',
'/oracle/product/9.2.0/bin/imp',
'execute' );
dbms_java.grant_permission
('PRODN',
'java.lang.RuntimePermission',
'writeFileDescriptor' );
end;
---------------------------------AS PRODN user
create or replace and compile
java source named "Util"
as
import java.io.*;
import java.lang.*;
public class Util extends Object
public static int RunThis(String args)
Runtime rt = Runtime.getRuntime();
int rc = -1;
try
Process p = rt.exec(args);
int bufSize = 4096;
BufferedInputStream bis = new BufferedInputStream(p.getInputStream(),
bufSize);
int len;
byte buffer[] = new byte[bufSize];
// Echo back what the program spit out
while ((len = bis.read(buffer, 0, bufSize)) != -1)
System.out.write(buffer, 0, len);
rc = p.waitFor();
catch (Exception e)
e.printStackTrace();
rc = -1;
finally
return rc;
create or replace
function RUN_CMD(p_cmd in varchar2) return number
as
language java
name 'Util.RunThis(java.lang.String) return integer';
create or replace procedure RC(p_cmd in varchar2)
as
x number;
begin
x := run_cmd(p_cmd);
end;
i executed the command as follows in sql>
BEGIN
rc('/oracle/product/9.2.0/bin/imp tim/tim file=/home/oracle/vas4359.dmp log
=log1.log');
END;
it runs fine creates log1.log in / home/oracle but there is no output in
log1.log file.
I have to kill the process as follows after which sql> displays me :
"PL/SQL procedure successfully completed."
[oracle@msebdb oracle]$ ps aux|grep imp
oracle 20052 0.0 0.2 12732 4328 ? S 11:57 0:00 /oracle/product/
.2.0/bin/imp file=/home/oracle/vas4359.dmp log=log1.log
oracle 20102 0.0 0.0 3692 652 pts/2 S 12:03 0:00 grep imp
Note:There is no tim user.. i just want to get error in my log file..
my further steps are to read this log file and send email/notifications to users.
my main program is as f/ws:
/*imp and check log file*/
create or replace procedure abc
as
v_exists boolean;
v_exists_log boolean;
ex_open BOOLEAN;
flen NUMBER;
bsize NUMBER;
vInHandle_open utl_file.file_type;
vInHandle_open_log utl_file.file_type;
v_filelog utl_file.file_type;
v_fl utl_file.file_type;
v_pth varchar2(60);
v_fnm varchar2(60);
v_file_exists varchar2(60);
v_logline varchar2(30) ;
v_search varchar2(30) :='ORA-';
vNewLine VARCHAR2(32767);
cursor c_fname is
select full_pth ,f_name from upld_file_t where f_name in
(select f_name from tbl_file_nm where typ_file='D' AND DEL_FLG='N');
BEGIN
for v_sec in c_fname
loop
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name,
FEXISTS=>v_exists,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists
THEN
dbms_output.put_line('File Exists' || v_sec.f_name);
v_file_exists:=v_sec.f_name;
--dbms_output.put_line('v_file_exists ' || v_file_exists);
dbms_output.put_line('File Length: ' || TO_CHAR(flen));
dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
dbms_output.put_line(' before file open');
vInHandle_open := utl_file.fopen('ORALOAD',v_sec.f_name,'R',32767);
-- dbms_output.put_line(' after file open');
-- do whater with teh file data
v_fl:=utl_file.fopen('ORALOAD','v_sec.f_name','W');
dbms_output.put_line('v_sec.f_name');
IF utl_file.is_open(vInHandle_open)
THEN
/*if log file exists..*/
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name||'.log',
FEXISTS=>v_exists_log,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists_log
then
dbms_output.put_line('file' ||v_sec.f_name||'.log');
v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','W',32767);
LOOP
BEGIN
UTL_FILE.PUT(v_filelog,'ORA-');
exit;
END;
END LOOP;
else
dbms_output.put_line('File not found' || v_sec.f_name);
END IF;
UTL_FILE.fclose(v_filelog);
dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
END IF;
/*imp THE FILE and read log file*/
utl_file.fgetattr(LOCATION=>'ORALOAD',
FILENAME=>v_sec.f_name||'.log',
FEXISTS=>v_exists_log,
FILE_LENGTH=> flen,
BLOCK_SIZE=>bsize);
if v_exists_log
then
dbms_output.put_line('file' ||v_sec.f_name||'.log');
v_filelog :=utl_file.fopen('ORALOAD',v_sec.f_name||'.log','R',32767);
LOOP
BEGIN
UTL_FILE.get_line(v_filelog,v_logline);
if v_logline = v_search then
dbms_output.put_line(v_logline);
end if;
UTL_FILE.fclose(v_filelog);
dbms_output.put_line('Closed File ' || v_sec.f_name|| '.log');
exit;
END;
END LOOP;
else
dbms_output.put_line('File not found' || v_sec.f_name);
END IF;
END IF;
END LOOP;
--close the curosr .. check this
EXCEPTION
WHEN UTL_FILE.ACCESS_DENIED THEN
DBMS_OUTPUT.PUT_LINE('No Access!!!');
when others then
dbms_output.put_line('ERROR (open_file) => '||sqlcode);
dbms_output.put_line('MSG (open_file) => '||sqlerrm);
end;Hi,
You might have better luck in the PL/SQL or Java forums. They are located here:
PL/SQL: PL/SQL
Java: http://forums.oracle.com/forums/forum.jsp?forum=99
Good luck,
Mark -
OK, i create a directory i have read/write permissions on the directory, but still even if the file exists its bringing back the result as if it doesnt i need help with this...
set serveroutput on
DECLARE
fexists BOOLEAN;
flen NUMBER;
bsize NUMBER;
BEGIN
sys.utl_file.fgetattr('TMP_LOCATION', 'test.txt', fexists, flen, bsize);
IF fexists THEN
dbms_output.put_line('File Exists');
ELSE
dbms_output.put_line('File Does Not Exist');
END IF;
dbms_output.put_line('File Length: ' || TO_CHAR(flen));
dbms_output.put_line('Block Size: ' || TO_CHAR(bsize));
END fgetattr;
File Does Not Existmy directory.. CREATE or replace DIRECTORY tmp_location AS 'Server-1\configsheets\completed\pdf approved'Good afternoon,
If you are loooking at a mapped drive, you can only see it if you change how the Oracle services start. You need to have them start with a specific user ID rather than starting as system services. The problem is that the system services cannot see mapped drives.
So, do this:
<li>Create a user to be used by Oracle services. Make sure they have the correct rights (I gave mine Server ADMIN so as to have all the access I needed)
<li>Log into the server and modify the services
<li>Click the 'Log On' tab and enter the account and password you created for the Oracle Processes
<li>Reboot the server, allowing the Oracle services to log in with this new ID.
This should then fix your problem.
Thanks,
Don.
REWARDS: Please remember to mark helpful or correct posts on the forum, not just for my answers but for everyone! :) -
File does not exist, SMTP permanent error
Hi All,
I got stuck with this error, please help me on this.
Mine is windows OS, apps 11.5.10.2
I have a cocncurrent program out file (.out) in out directory and also the same file with naming convention 'name_concurrenrequestid_1.EXCEL' extension in the same directoy. I need to send this EXCEL file as attachment to email id in windows environment. Whenver submit the request is giving the error.
File Does Not Exist
Error in Send Mail: ORA-29279: SMTP permanent error: 501 5.5.4 Invalid Address.
I verified the file , it is already existed in the directory. Why did it not recognized the file. I tried the below query in toad,, but got the 'FALSE' for %APPLCSF, and got the TRUE for custom directory which has the same file
declare
v_boolean BOOLEAN;
v_Len NUMBER;
v_Block NUMBER;
BEGIN
utl_file.fgetattr('%APPLCSF%\%APPLOUT', 'name_concurrentreuestid_1.EXCEL', v_boolean, v_Len, v_Block);
if v_boolean then
dbms_output.put_line('v_boolean: TRUE');
dbms_output.put_line('L:' || to_char(v_Len));
dbms_output.put_line('B:' || to_char(v_Block));
else
dbms_output.put_line('EX: FALSE');
end if;
END;
I do have full access on this folder. Once it recognizes the file I can send the mail.
Appreciate any help on this. Plz...........
Thanks,
JPThank you for the quick reply Srini.
But my email program is working fine if I change the path from %APPLCSF%APPLOUT to Customtop\out directory to identify the file. It was sent the attachment also to my email id.
But my requirement is I need to send the file from Out directory not from customtop out . Even it is not recognized the .out file using this UTL_File.fgetattr() in out directory.
Appreciate any furthur assistance.
Thanks,
JP
Maybe you are looking for
-
Shipping Point Determination for Sub contracting Process
Dear Experts, While processing the GR for a Sub Contract Purchase Order, we are getting an Error 'VL 100 - Shipping Point XXX doesn't exists' However, I am not understanding where from this Shipping point is picked. Checked the Shipping point determ
-
How to do a clean windows 7 re- install in a satellite L675D-S7046
I did the 4 recovery discs when first i used my laptop a year ago and some how i put the first disc in the cd room and nothing hapen. i just burn new 4 recovery discs and still i put it in but nothing happen. please help i want to do a clean install
-
Cannot open 5D Mark III raw files in CS6
I am running CS6 from disc. I just got a new laptop. Its a Toshiba Qosmio. I have loaded the whole program from disc and every time I try to open a raw file, It comes up with a message that I need to update Camera Raw. I have done all the updates an
-
Completion Workflow WS14000044
Hello, We are in the process of migrating from SRM4 (Standalone Scenario) to SRM7. We need to update the existing Shopping Cart approval workflow by adding new approver levels. Currently we are using a customized version of the n-step dynamic approva
-
Sqlloader with same csv files - 1 fails the other works fine!
Hi, I am using sqlldr to load data from a csv file into a table. The table has 23 columns in it. The last column is nullable. The CSV has got 23 column values for all the records excepting couple of records which have 22 values leaving the last 23rd