Get
Directory listing using Oracle PLSQL or Java
1.
Using PLSQL
By using PLSQL, we use DBMS_BACKUP_RESTORE package which is created
by the dbmsbkrs.sql and prvtbkrs.plb scripts and owned by SYS user.
This package, along with the target database version of DBMS_RCVMAN,
is automatically installed in every Oracle database when the
catproc.sql script is run. This package interfaces with the Oracle
database server and the operating system to provide the I/O services
for backup and restore operations as directed by RMAN.
Since DBMS_BACKUP_RESTORE package is API to RMAN functionaliy, care
must be taken while granting EXECUTE on DBMS_BACKUP_RESTORE package.
CREATE OR REPLACE PROCEDURE
list_directory
(directory VARCHAR2)
IS
ns
VARCHAR2(1024);
v_directory VARCHAR2(1024);
BEGIN
v_directory := directory;
SYS.DBMS_BACKUP_RESTORE.SEARCHFILES(v_directory, ns);
FOR each_file IN (SELECT
fname_krbmsft AS name FROM x$krbmsft) LOOP
DBMS_OUTPUT.PUT_LINE(each_file.name);
END LOOP;
END;
/
SQL> SET SERVEROUTPUT ON;
SQL> EXECUTE
sys.list_directory('/u01/app/oracle/admin/testdb/bdump');
/u01/app/oracle/admin/testdb/bdump/alert_testdb.LOG
/u01/app/oracle/admin/testdb/bdump/testdb_lgwr_6884.trc
/u01/app/oracle/admin/testdb/bdump/testdb_lgwr_10639.trc
/u01/app/oracle/admin/testdb/bdump/testdb_lgwr_10724.trc
/u01/app/oracle/admin/testdb/bdump/testdb_lgwr_3618.trc
/u01/app/oracle/admin/testdb/bdump/testdb_lgwr_10785.trc
Note: The following procedure must be compile with SYS user however
you can grant EXECUTE on list_directory to other users later, if not
you'll get
'PLS-00201: identifier 'SYS.DBMS_BACKUP_RESTORE' must be declared'
error.
SQL> GRANT EXECUTE ON
list_directory TO John;
2.
Using Java
-- Granting privillege
SQL> GRANT JAVAUSERPRIV TO
John;
-- Creating temporary table to hold the directory list.
CREATE global temporary TABLE
directory_listing (
name VARCHAR2(1024) )
ON COMMIT DELETE rows;
-- Creating java source name
CREATE OR REPLACE
AND compile JAVA source
named "Directory_Listing"
AS
import JAVA.io.*;
import JAVA.SQL.*;
PUBLIC class
Directory_Listing {
PUBLIC static void GetList(String directory) throws SQLException {
File PATH = NEW File( directory );
String[] list = PATH.list();
String element;
FOR(int i = 0; i < list.LENGTH; i++) {
element = list[i];
#sql { INSERT INTO directory_listing (name) VALUES (:element) };
}
}
}
/
-- Creating list_directory procedure to fillup temporary table
directory_listing.
CREATE OR REPLACE PROCEDURE
list_directory ( directory IN VARCHAR2 )
AS language JAVA
name
'Directory_Listing.GetList( java.lang.String )';
/
-- Executing Command
SQL> exec list_directory(
'/u01/app/oracle/admin/testdb/bdump' );
SQL> SELECT * FROM
directory_listing;
NAME
--------------------------------------------------------------------------------
alert_testdb.LOG
testdb_lgwr_6884.trc
testdb_lgwr_10639.trc
testdb_lgwr_10724.trc
testdb_lgwr_3618.trc
testdb_lgwr_10785.trc