List Files in a Directory From PL/SQL and SQL : DBMS_BACKUP_RESTORE
This article shows how to list files in a directory on the database server using the DBMS_BACKUP_RESTORE package.
oracle miscconfigurationintermediate
by OracleDba
14 views
This article shows how to list files in a directory on the database server using the DBMS_BACKUP_RESTORE package.
12345
CONN sys/SysPassword1@//localhost:1521/pdb1 AS SYSDBA
--DROP USER testuser1 CASCADE;
CREATE USER testuser1 IDENTIFIED BY testuser1 QUOTA UNLIMITED ON users;
GRANT CREATE SESSION, CREATE VIEW TO testuser1;12345678910111213141516171819202122232425262728293031323334
CREATE OR REPLACE TYPE sys.ob_varchar2_arr AS TABLE OF VARCHAR2(500);
/
CREATE OR REPLACE FUNCTION sys.ob_get_files (p_pattern IN VARCHAR2,
p_file_separator IN VARCHAR2 := '/')
RETURN sys.ob_varchar2_arr PIPELINED
AS
l_pattern VARCHAR2(32767);
l_ns VARCHAR2(32767);
BEGIN
-- Make sure the pattern ends with the file separator.
l_pattern := RTRIM(p_pattern, p_file_separator) || p_file_separator;
sys.DBMS_BACKUP_RESTORE.searchfiles(
pattern => l_pattern,
ns => l_ns,
onlyfnm => TRUE,
normfnm => TRUE);
-- Pull back all files directly under specified directory.
-- WHERE filter removes recursion.
FOR cur_rec IN (SELECT fname_krbmsft
FROM sys.x$krbmsft
WHERE INSTR(SUBSTR(fname_krbmsft, LENGTH(l_pattern)+1), p_file_separator) = 0)
LOOP
-- Display the file name without the preceding path.
PIPE ROW(SUBSTR(cur_rec.fname_krbmsft, LENGTH(l_pattern)+1));
END LOOP;
RETURN;
END;
/
GRANT EXECUTE ON sys.ob_varchar2_arr TO testuser1;
GRANT EXECUTE ON sys.ob_get_files TO testuser1;1234567891011121314151617181920212223242526272829303132
CONN testuser1/testuser1@//localhost:1521/pdb1
COLUMN file_name FORMAT A30
SELECT column_value AS file_name
FROM TABLE(sys.ob_get_files('/u01/app/oracle/diag/rdbms/cdb1/cdb1/trace'));
FILE_NAME
------------------------------
cdb1_ora_15109.trc
cdb1_ora_15109.trm
alert_cdb1.log
... removed for brevity ...
cdb1_ora_20934.trm
cdb1_m004_20954.trc
cdb1_m004_20954.trm
481 rows selected.
SQL>
SELECT column_value AS file_name
FROM TABLE(sys.ob_get_files('/u01/app/oracle/diag/rdbms/cdb1/cdb1/trace'))
WHERE column_value LIKE 'alert%';
FILE_NAME
------------------------------
alert_cdb1.log
SQL>12345678
CREATE OR REPLACE VIEW trace_files_v AS
SELECT column_value AS file_name
FROM TABLE(sys.ob_get_files('/u01/app/oracle/diag/rdbms/cdb1/cdb1/trace'));
CREATE OR REPLACE VIEW alert_log_v AS
SELECT column_value AS file_name
FROM TABLE(sys.ob_get_files('/u01/app/oracle/diag/rdbms/cdb1/cdb1/trace'))
WHERE column_value LIKE 'alert%';Please to add comments
No comments yet. Be the first to comment!