Search This Blog

Thursday, July 28, 2022

Get file names in an Oracle Directory using Java in Oracle PL/SQL

How to read multiple files/folders from an Oracle Directory using Java in Oracle PL/SQL?

1. Create a COMPILE JAVA SOURCE NAMED using below code (.cdb in IFS)

--
-- Module : MODULE_NAME
--
-- Filename : FILE_NAME
--
-- Purpose : Access files in oracle directory
--
--  Date    Sign     History
--  ------  -------  --------------------------------------------------------
--  
-----------------------------------------------------------------------------

SET SERVEROUTPUT ON SIZE 32000

PROMPT Starting FILE_NAME...

CREATE OR REPLACE AND COMPILE JAVA SOURCE NAMED "RmaxFileHandler" AS
import java.io.*;
import java.util.*;

public class RmaxFileHandler {
    private static String SEMICOLON = ";";
    private static int VARCHAR2_MAX_LENGTH = 3000;
    private static int VARCHAR2_SAFETY_BUFFER = 1000;

    // Private Functions - START
    private static ArrayList getFilePaths(String oracleDirectoryPath, String folderSeparator, boolean includeSubFolders) {
        ArrayList filePathList = new ArrayList();

        try {
            String searchFolderSeparator = getSearchFolderSeparator(folderSeparator);

            //Creating a File object for directory
            File directoryPath = new File(oracleDirectoryPath);

            //List of all files and directories
            String contents[] = directoryPath.list();

            for(int i=0; i getRelativeFilePaths(String oracleDirectoryPath, String folderSeparator, boolean includeSubFolders) {
        ArrayList relativeFilePaths = getFilePaths(oracleDirectoryPath, folderSeparator, includeSubFolders);
        String searchFolderSeparator = getSearchFolderSeparator(folderSeparator);

        // set correct folder separator in oracleDirectoryPath
        oracleDirectoryPath = oracleDirectoryPath.replace(searchFolderSeparator, folderSeparator);

        for (int i = 0; i < relativeFilePaths.size(); i++) {
            // get file path
            String filePath = relativeFilePaths.get(i);

            // set relative file path
            String relativeFilePath = filePath.replace(oracleDirectoryPath, "");

            // get file paths
            relativeFilePaths.set(i, relativeFilePath);
        }

        return relativeFilePaths;
    }

    private static ArrayList getFileNames(String oracleDirectoryPath, String folderSeparator, boolean includeSubFolders) {
        ArrayList filePathList = new ArrayList();

        try {
            String searchFolderSeparator = getSearchFolderSeparator(folderSeparator);

            //Creating a File object for directory
            File directoryPath = new File(oracleDirectoryPath);

            //List of all files and directories
            String contents[] = directoryPath.list();

            for(int i=0; i getFolderNames(String oracleDirectoryPath, String folderSeparator, boolean includeSubFolders) {
        ArrayList filePathList = new ArrayList();

        try {
            String searchFolderSeparator = getSearchFolderSeparator(folderSeparator);

            //Creating a File object for directory
            File directoryPath = new File(oracleDirectoryPath);

            //List of all files and directories
            String contents[] = directoryPath.list();

            for(int i=0; i filePaths, Integer lastFilePathId) {
        int startIndex = -1;
        String strFilePathList = "";

        if (lastFilePathId == -1) {
            startIndex = 0;
        } else {
            startIndex = (lastFilePathId + 1);
        }

        for (int i = startIndex; i < filePaths.size(); i++) {
            // Prevent directory listing expanding if we will blow VARCHAR2 limit.
            if ((strFilePathList.length() + filePaths.get(i).length() + VARCHAR2_SAFETY_BUFFER) > VARCHAR2_MAX_LENGTH) {
                strFilePathList += "$$" + (i - 1) + "$$";
                break;
            }

            strFilePathList += filePaths.get(i) + SEMICOLON;
        }

        return strFilePathList;
    }
    // Private Functions - FINISH


    // Public Functions - START
    public static String getFilePathsToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFilePathId) {
        ArrayList filePaths = getFilePaths(oracleDirectoryPath, folderSeparator, false);
        return concatFilePaths(filePaths, lastFilePathId);
    }

    public static String getAllFilePathsToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFilePathId) {
        ArrayList filePaths = getFilePaths(oracleDirectoryPath, folderSeparator, true);
        return concatFilePaths(filePaths, lastFilePathId);
    }

    public static String getRelativeFilePathsToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFilePathId) {
        ArrayList relativeFilePaths = getRelativeFilePaths(oracleDirectoryPath, folderSeparator, false);
        return concatFilePaths(relativeFilePaths, lastFilePathId);
    }

    public static String getAllRelativeFilePathsToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFilePathId) {
        ArrayList relativeFilePaths = getRelativeFilePaths(oracleDirectoryPath, folderSeparator, true);
        return concatFilePaths(relativeFilePaths, lastFilePathId);
    }

    public static String getFileNamesToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFileNameId) {
        ArrayList filePaths = getFileNames(oracleDirectoryPath, folderSeparator, false);
        return concatFilePaths(filePaths, lastFileNameId);
    }

    public static String getAllFileNamesToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFileNameId) {
        ArrayList filePaths = getFileNames(oracleDirectoryPath, folderSeparator, true);
        return concatFilePaths(filePaths, lastFileNameId);
    }

    public static String getFolderNamesToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFolderNameId) {
        ArrayList filePaths = getFolderNames(oracleDirectoryPath, folderSeparator, false);
        return concatFilePaths(filePaths, lastFolderNameId);
    }

    public static String getAllFolderNamesToClob(String oracleDirectoryPath, String folderSeparator, Integer lastFolderNameId) {
        ArrayList filePaths = getFolderNames(oracleDirectoryPath, folderSeparator, true);
        return concatFilePaths(filePaths, lastFolderNameId);
    }
    // Public Functions - FINISH
};
/

SET SERVEROUTPUT OFF;

PROMPT Finished with FILE_NAME...

2. Create Oracle Package Spec (.api in IFS)

-----------------------------------------------------------------------------
--
--  Logical unit: RmaxFileHandler
--
--  Date    Sign     History
--  ------  -------  --------------------------------------------------------
--  
-----------------------------------------------------------------------------

DEFINE MODULE        = RMAX
DEFINE LU            = RmaxFileHandler
DEFINE PKG           = RMAX_FILE_HANDLER_API


PROMPT Creating &PKG specification

CREATE OR REPLACE PACKAGE &PKG IS

module_  CONSTANT VARCHAR2(25) := '&MODULE';
lu_name_ CONSTANT VARCHAR2(25) := '&LU';

-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PRIVATE METHODS ----------------------------
-----------------------------------------------------------------------------

FUNCTION Get_File_Paths_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_path_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getFilePathsToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';

FUNCTION Get_All_File_Paths_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_path_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getAllFilePathsToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_Relative_File_Paths_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_path_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getRelativeFilePathsToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_All_Rel_File_Paths_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_path_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getAllRelativeFilePathsToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_File_Names_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_name_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getFileNamesToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_All_File_Names_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_file_name_id_     IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getAllFileNamesToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_Folder_Names_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_folder_name_id_   IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getFolderNamesToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';


FUNCTION Get_All_Folder_Names_Java__ (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2,
   last_folder_name_id_   IN NUMBER ) RETURN VARCHAR2
AS LANGUAGE JAVA 
NAME 'RmaxFileHandler.getAllFolderNamesToClob (java.lang.String, java.lang.String, java.lang.Integer) return java.lang.String';

-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PROTECTED METHODS --------------------------
-----------------------------------------------------------------------------


-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PUBLIC METHODS -----------------------------
-----------------------------------------------------------------------------

-- Get full file paths of the specified oracle directory
FUNCTION Get_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get full file paths of the specified oracle directory and subdirectories
FUNCTION Get_All_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get relative file paths of the specified oracle directory
FUNCTION Get_Relative_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get relative file paths of the specified oracle directory and subdirectories
FUNCTION Get_All_Relative_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get file names of the specified oracle directory
FUNCTION Get_File_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get file names of the specified oracle directory and subdirectories
FUNCTION Get_All_File_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get folder names of the specified oracle directory
FUNCTION Get_Folder_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- Get folder names of the specified oracle directory and subdirectories
FUNCTION Get_All_Folder_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB;


-- A sample test procedure for above all functions
PROCEDURE Sample_Test;

-----------------------------------------------------------------------------
-------------------- FOUNDATION1 METHODS ------------------------------------
-----------------------------------------------------------------------------

PROCEDURE Init;


END &PKG;
/
SHOW ERROR

UNDEFINE MODULE
UNDEFINE LU
UNDEFINE PKG
-----------------------------------------------------------------------------

3. Create Create Oracle Package Body (.apy in IFS)

-----------------------------------------------------------------------------
--
--  Logical unit: RmaxFileHandler
--
--  Purpose: LU for Handle Files in Oracle Directory
--
--  Date    Sign     History
--  ------  -------  --------------------------------------------------------
--  
-----------------------------------------------------------------------------

DEFINE MODULE        = RMAX
DEFINE LU            = RmaxFileHandler
DEFINE PKG           = RMAX_FILE_HANDLER_API

-----------------------------------------------------------------------------
-------------------- PACKAGES FOR METHODS -----------------------------------
-----------------------------------------------------------------------------

PROMPT Creating &PKG implementation

CREATE OR REPLACE PACKAGE BODY &PKG IS

-----------------------------------------------------------------------------
-------------------- GLOBAL LU CONSTANTS ------------------------------------
-----------------------------------------------------------------------------

JAVA_GET_FILE_PATHS           CONSTANT VARCHAR2(30) := 'Get_File_Paths_Java__';
JAVA_GET_ALL_FILE_PATHS       CONSTANT VARCHAR2(30) := 'Get_All_File_Paths_Java__';
JAVA_GET_REL_FILE_PATHS       CONSTANT VARCHAR2(30) := 'Get_Relative_File_Paths_Java__';
JAVA_GET_ALL_REL_FILE_PATHS   CONSTANT VARCHAR2(30) := 'Get_All_Rel_File_Paths_Java__';
JAVA_GET_FILE_NAMES           CONSTANT VARCHAR2(30) := 'Get_File_Names_Java__';
JAVA_GET_ALL_FILE_NAMES       CONSTANT VARCHAR2(30) := 'Get_All_File_Names_Java__';
JAVA_GET_FOLDER_NAMES         CONSTANT VARCHAR2(30) := 'Get_Folder_Names_Java__';
JAVA_GET_ALL_FOLDER_NAMES     CONSTANT VARCHAR2(30) := 'Get_All_Folder_Names_Java__';

-----------------------------------------------------------------------------
-------------------- LU SPECIFIC IMPLEMENTATION METHOD DECLARATIONS ---------
-----------------------------------------------------------------------------

FUNCTION Retrieve_File_Paths___ (
   java_function_         IN VARCHAR2,
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 ) RETURN CLOB;
  
-----------------------------------------------------------------------------
-------------------- LU SPECIFIC IMPLEMENTATION METHODS ---------------------
-----------------------------------------------------------------------------

FUNCTION Retrieve_File_Paths___ (
   java_function_         IN VARCHAR2,
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 ) RETURN CLOB
IS
   file_paths_       CLOB;
   last_file_path_   NUMBER;
   has_more_files_   NUMBER;
   
   CURSOR get_last_file_path IS
      SELECT TO_NUMBER(SUBSTR(SUBSTR(file_paths_, (INSTR(file_paths_, '$$') + 2)), 1, (LENGTH(SUBSTR(file_paths_, (INSTR(file_paths_, '$$') + 2))) - 2)))
      FROM   DUAL;
   
   FUNCTION Get_Correct_File_Paths (
      temp_file_paths_ IN CLOB ) RETURN CLOB
   IS
   BEGIN
      RETURN SUBSTR(temp_file_paths_, 1, (INSTR(temp_file_paths_, '$$') - 1));
   END;
BEGIN
   last_file_path_ := -1;
   
   IF (java_function_ = JAVA_GET_FILE_PATHS) THEN
      file_paths_ := Get_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_ALL_FILE_PATHS) THEN
      file_paths_ := Get_All_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_REL_FILE_PATHS) THEN
      file_paths_ := Get_Relative_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_ALL_REL_FILE_PATHS) THEN
      file_paths_ := Get_All_Rel_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_FILE_NAMES) THEN
      file_paths_ := Get_File_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_ALL_FILE_NAMES) THEN
      file_paths_ := Get_All_File_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_FOLDER_NAMES) THEN
      file_paths_ := Get_Folder_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   ELSIF (java_function_ = JAVA_GET_ALL_FOLDER_NAMES) THEN
      file_paths_ := Get_All_Folder_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
   END IF;
   
   WHILE (INSTR(file_paths_, '$$') > 0) LOOP
      OPEN  get_last_file_path;
      FETCH get_last_file_path INTO last_file_path_;
      IF (get_last_file_path%NOTFOUND) THEN
         last_file_path_ := -1;
      END IF;
      CLOSE get_last_file_path;
      
      file_paths_ := Get_Correct_File_Paths(file_paths_);
      
      IF (java_function_ = JAVA_GET_FILE_PATHS) THEN
         file_paths_ := file_paths_ || Get_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_ALL_FILE_PATHS) THEN
         file_paths_ := file_paths_ || Get_All_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_REL_FILE_PATHS) THEN
         file_paths_ := file_paths_ || Get_Relative_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_ALL_REL_FILE_PATHS) THEN
         file_paths_ := file_paths_ || Get_All_Rel_File_Paths_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_FILE_NAMES) THEN
         file_paths_ := file_paths_ || Get_File_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_ALL_FILE_NAMES) THEN
         file_paths_ := file_paths_ || Get_All_File_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_FOLDER_NAMES) THEN
         file_paths_ := file_paths_ || Get_Folder_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      ELSIF (java_function_ = JAVA_GET_ALL_FOLDER_NAMES) THEN
         file_paths_ := file_paths_ || Get_All_Folder_Names_Java__(oracle_directory_path_, NVL(folder_separator_, '/'), last_file_path_);
      END IF;
   END LOOP;
   
   RETURN file_paths_;
END Retrieve_File_Paths___;

-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PRIVATE METHODS ----------------------------
-----------------------------------------------------------------------------


-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PROTECTED METHODS --------------------------
-----------------------------------------------------------------------------


-----------------------------------------------------------------------------
-------------------- LU SPECIFIC PUBLIC METHODS -----------------------------
-----------------------------------------------------------------------------

-- Get full file paths of the specified oracle directory
FUNCTION Get_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_FILE_PATHS, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_File_Paths;


-- Get full file paths of the specified oracle directory and subdirectories
FUNCTION Get_All_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_ALL_FILE_PATHS, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_All_File_Paths;


-- Get relative file paths of the specified oracle directory
FUNCTION Get_Relative_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_REL_FILE_PATHS, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_Relative_File_Paths;


-- Get relative file paths of the specified oracle directory and subdirectories
FUNCTION Get_All_Relative_File_Paths (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_ALL_REL_FILE_PATHS, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_All_Relative_File_Paths;


-- Get file names of the specified oracle directory
FUNCTION Get_File_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_FILE_NAMES, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_File_Names;


-- Get file names of the specified oracle directory and subdirectories
FUNCTION Get_All_File_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_ALL_FILE_NAMES, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_All_File_Names;


-- Get folder names of the specified oracle directory
FUNCTION Get_Folder_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_FOLDER_NAMES, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_Folder_Names;


-- Get folder names of the specified oracle directory and subdirectories
FUNCTION Get_All_Folder_Names (
   oracle_directory_path_ IN VARCHAR2, 
   folder_separator_      IN VARCHAR2 DEFAULT '/' ) RETURN CLOB
IS
BEGIN
   RETURN Retrieve_File_Paths___(JAVA_GET_ALL_FOLDER_NAMES, oracle_directory_path_, NVL(folder_separator_, '/'));
END Get_All_Folder_Names;


-- A sample test procedure for above all functions
PROCEDURE Sample_Test 
IS
   file_paths_        CLOB;
   file_path_value_   VARCHAR2(4000);
   directory_path_    VARCHAR2(1000);
   start_pos_         NUMBER;
   delimiter_pos_     NUMBER;
   delimiter_         VARCHAR2(1) := ';';
   counter_           NUMBER;
   
   CURSOR get_directory_path IS 
      SELECT directory_path
      FROM   all_directories
      WHERE  directory_name = 'NAME_OF_ORACLE_DIRECTORY';
   
   CURSOR get_file_path IS
      SELECT regexp_substr(file_paths_, '[^;]+', 1, level) AS split_value
      FROM dual
      CONNECT BY regexp_substr(file_paths_, '[^;]+', 1, level) IS NOT NULL;
BEGIN
   OPEN  get_directory_path;
   FETCH get_directory_path INTO directory_path_;
   CLOSE get_directory_path;
   
   file_paths_ := Rmax_File_Handler_API.Get_File_Paths(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_All_File_Paths(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_Relative_File_Paths(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_All_Relative_File_Paths(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_File_Names(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_All_File_Names(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_Folder_Names(directory_path_, '/');
   --file_paths_ := Rmax_File_Handler_API.Get_All_Folder_Names(directory_path_, '/');
   
   --Dbms_Output.Put_Line(file_paths_);
   
   start_pos_     := 1;
   delimiter_pos_ := 1;
   counter_       := 1;
   
   WHILE (delimiter_pos_ <= LENGTH(file_paths_)) LOOP
      delimiter_pos_   := INSTR(file_paths_, delimiter_, start_pos_);
      file_path_value_ := SUBSTR(file_paths_, start_pos_, (delimiter_pos_ - start_pos_));
      
      Dbms_Output.Put_Line(LPAD(counter_, 5, '0') || '. ' || file_path_value_);
      
      IF (delimiter_pos_ = LENGTH(file_paths_)) THEN
         EXIT;
      END IF;
      
      start_pos_ := delimiter_pos_ + 1;
      counter_   := counter_ + 1;
   END LOOP;
END Sample_Test;

-----------------------------------------------------------------------------
-------------------- FOUNDATION1 METHODS ------------------------------------
-----------------------------------------------------------------------------
-- Init
--   Dummy procedure that can be called at database startup to ensure that
--   this package is loaded into memory for performance reasons only.
-----------------------------------------------------------------------------

PROCEDURE Init
IS
BEGIN
   NULL;
END Init;

END &PKG;
/
SHOW ERROR

UNDEFINE MODULE
UNDEFINE LU
UNDEFINE PKG
-----------------------------------------------------------------------------

4. Testing

BEGIN
  -- Call the procedure
  Rmax_File_Handler_API.Sample_Test;
END;

Useful Links:

> File Handling From PL/SQL

Related Links:

> Get file names in Oracle Directory in IFS without using Complied Java Source

No comments:

Post a Comment

Read file content from an Oracle Directory in PL/SQL/ in IFS

Read single file DECLARE file_handle_ UTL_FILE.FILE_TYPE; directory_name_ CONSTANT all_directories.directory_name%TYPE := '...

Popular Posts