Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

problem in finding list of files in directory

I want to retrieve list of all file in a specific folder that included oracle form and menu and report and some txt file...

Do you have any idea how I can retrieve these data in ORACLE form, and insert them into my data block, automatically?

I use oracle form 6.0.

like image 876
Amir Avatar asked Dec 10 '22 12:12

Amir


2 Answers

I did something along these lines:

Create an Oracle directory for the directory you want to list:

create or replace directory YOURDIR
  as '\path\to\your\directory';

Build a temporary table:

create global temporary table DIR_LIST
(
  FILENAME VARCHAR2(255),
)
on commit preserve rows;
grant select, insert, update, delete on DIR_LIST to PUBLIC;

You'll need a java stored procedure:

create or replace and compile java source named dirlist as
import java.io.*;
  import java.sql.*;
  import java.text.*;

  public class DirList
  {
  public static void getList(String directory)
                     throws SQLException
  {
      File dir = new File( directory );
      File[] files = dir.listFiles();
      File theFile;

      for(int i = 0; i < files.length; i++)
      {
          theFile = files[i];
          #sql { INSERT INTO DIR_LIST (FILENAME)
                 VALUES (:theName };
      }
  }

  }

And a PL/SQL callable procedure to invoke the java:

CREATE OR REPLACE PROCEDURE get_dir_list(pi_directory IN VARCHAR2)
AS LANGUAGE JAVA
name 'DirList.getList(java.lang.String)';

Finally, calling the procedure get_dir_list inside your form will populate the table with the files in your directory, which you can then read into your form block.

The java code came straight out of a Tom Kyte book (don't recall which one).

EDIT:

Actually, all the code is pretty much lifted from this AskTom thread.

like image 172
DCookie Avatar answered Dec 14 '22 22:12

DCookie


There is another interesting approach with external tables that makes it even easier to retrieve such lists without using a Java stored procedure:

$ mkdir /tmp/incoming

$ cat >/tmp/incoming/readdir.sh<<eof
#/bin/bash
cd /tmp/incoming/
/bin/ls -1
eof

# test files
$ for i in {1..5}; do touch /tmp/incoming/invoice_no_$RANDOM.pdf; done

In SQL*Plus:

create or replace directory incoming as '/tmp/incoming';

Directory INCOMMING created.

create table files (filename varchar2(255))
organization external ( 
    type oracle_loader
    default directory incoming
    access parameters (
        records delimited by newline
        preprocessor  incoming:'readdir.sh'
        fields terminated by "|" ldrtrim
    )
location ('readdir.sh')
);
/

Table FILES created.

select * from files;

FILENAME                                                                       
--------------------------------------------------------------------------------
FILES_27463.log                                                                 
invoice_no_20891.pdf                                                            
invoice_no_2255.pdf                                                             
invoice_no_24086.pdf                                                            
invoice_no_30372.pdf                                                            
invoice_no_8340.pdf                                                             
readdir.sh                                                                      

 7 rows selected 

This approach was added in the same Ask Tom thread as mentioned in the @DCookie's answere.

like image 26
0xdb Avatar answered Dec 14 '22 22:12

0xdb