Home » Developer & Programmer » JDeveloper, Java & XML » How to get list of files in a server DIRECTORY.
icon7.gif  How to get list of files in a server DIRECTORY. [message #174007] Thu, 25 May 2006 09:00 Go to next message
icemelid
Messages: 15
Registered: April 2005
Location: BARCELONA
Junior Member

Dear Sirs,

I have been going throught the wellknown piece of code below, to get the file's list in a directory on the server, but although it all compiles, I do not get the table fullfilled
with the files in the directory I require.

Could you help me to get the answer, what can I try or where
to look into ?

Thanks in advance.


==============================================

GRANT JAVAUSERPRIV to public;

drop table DIR_LIST;
create global temporary table DIR_LIST (
filename varchar2(255),
lastmodified date,
FileType char(1)
) on commit delete rows;

grant all on DIR_LIST to public;
create or replace public synonym DIR_LIST for DIR_LIST;

create or replace and compile java source named "DirList"
as
import java.io.*;
import java.sql.*;

public class DirList
{

static private String dateStr( java.util.Date x )
{
if ( x != null )
return (x.getYear()+1900) + "/" + (x.getMonth()+1) + "/" + x.getDate() + " " + x.getHours() + ":" + x.getMinutes() + ":" + x.getSeconds();
else return null;
}

public static void getList(String directory)
throws SQLException
{
String element;


File path = new File(directory);
File[] FileList = path.listFiles();
String TheFile;
String ModiDate;
String FileType;
#sql { DELETE FROM DIR_LIST};

for(int i = 0; i < FileList.length; i++)
{
TheFile = FileList[i].getName();
ModiDate = dateStr( new java.util.Date( FileList[i].lastModified() ) );
FileType = "-";
if ( FileList[i].isDirectory() ) FileType = "D";
if ( FileList[i].isFile() ) FileType = "F";

#sql { INSERT INTO DIR_LIST (FILENAME,LASTMODIFIED,FILETYPE) VALUES (:TheFile, to_date( :ModiDate, 'yyyy/mm/dd hh24:mi:ss'), :FileType ) };
}
}
}
/

show err

create or replace procedure get_dir_list( p_directory in varchar2 )
as language java
name 'DirList.getList( java.lang.String )'
/

grant execute on get_dir_list to public;
create or replace public synonym get_dir_list for get_dir_list;

exec dbms_java.grant_permission( 'SCOTT', 'SYS:java.io.FilePermission', 'c:\*', 'read' )


-- Handling:
-- exec dbms_java.set_output(100000);
-- exec get_dir_list( 'c:\temp' );
-- select * from dir_list;
-- commit;


-- Example:
SQL*Plus: Release 9.2.0.1.0 - Production on Di Aug 16 08:46:18 2005

Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.


SQL> exec get_dir_list('\\192.168.1.100\import\bd_t_20050808.lis');
BEGIN get_dir_list('\\192.168.1.100\import\bd_t_20050808.lis'); END;

*
FEHLER in Zeile 1:
ORA-29532: Java-Aufruf durch nicht abgefangene Java-Exception beendet:
java.lang.NullPointerException
ORA-06512: in "SYSTEM.GET_DIR_LIST", Zeile 0
ORA-06512: in Zeile 1


SQL> exec get_dir_list('d:\temp');

PL/SQL-Prozedur wurde erfolgreich abgeschlossen.

SQL> set linesize 900
SQL> select * from dir_list;

Empty list << -------- ? ----------- >>



Re: How to get list of files in a server DIRECTORY. [message #174008 is a reply to message #174007] Thu, 25 May 2006 09:05 Go to previous messageGo to next message
aciolac
Messages: 242
Registered: February 2006
Senior Member
Are You commit changes??? I have not see commit in text of procedure.
Commit seems to do nothing [message #174021 is a reply to message #174007] Thu, 25 May 2006 10:26 Go to previous messageGo to next message
icemelid
Messages: 15
Registered: April 2005
Location: BARCELONA
Junior Member

Yes, in fact, as I do not leave the session when doing
all these, no commit is required.

Though that, yes, I tried COMMIT, and nothings happen.

Regards
Ignasi
Re: How to get list of files in a server DIRECTORY. [message #174022 is a reply to message #174007] Thu, 25 May 2006 10:38 Go to previous messageGo to next message
aciolac
Messages: 242
Registered: February 2006
Senior Member
Try to see if procedure really reads file list. For this set serveroutput on and put some output in program, for example:

for(int i = 0; i < FileList.length; i++)
{
TheFile = FileList[i].getName();
ModiDate = dateStr( new java.util.Date( FileList[i].lastModified() ) );
FileType = "-";
if ( FileList[i].isDirectory() ) FileType = "D";
if ( FileList[i].isFile() ) FileType = "F";
//Print file name here
#sql { INSERT INTO DIR_LIST (FILENAME,LASTMODIFIED,FILETYPE) VALUES (:TheFile, to_date( :ModiDate, 'yyyy/mm/dd hh24:mi:ss'), :FileType ) };
}
Re: How to get list of files in a server DIRECTORY. [message #174026 is a reply to message #174022] Thu, 25 May 2006 11:19 Go to previous messageGo to next message
icemelid
Messages: 15
Registered: April 2005
Location: BARCELONA
Junior Member

Hi aciolac

I did as you said:


System.out.println("----------> ENTRA ");

for(int i = 0; i < FileList.length; i++)
{
TheFile = FileList[i].getName();
ModiDate = dateStr( new java.util.Date( FileList[i].lastModified() ) );
FileType = "-";
if ( FileList[i].isDirectory() ) FileType = "D";
if ( FileList[i].isFile() ) FileType = "F";

System.out.println("----------> "+ModiDate);

#sql { INSERT INTO DIR_LIST (FILENAME,LASTMODIFIED,FILETYPE) VALUES (:TheFile, to_date( :ModiDate, 'yyyy/mm/dd hh24:mi:ss'), :FileType ) };

}

But, only shows "ENTRA"

So, It does not reach any directory on the server.

I dont know.

Thanks
Ignasi



Re: How to get list of files in a server DIRECTORY. [message #174027 is a reply to message #174026] Thu, 25 May 2006 11:23 Go to previous messageGo to next message
aciolac
Messages: 242
Registered: February 2006
Senior Member
So, we find the main error Smile I think that there are necessary to define correct objects in Java. Try so see at last one example which read file list from server.
Re: How to get list of files in a server DIRECTORY. [message #174031 is a reply to message #174027] Thu, 25 May 2006 11:40 Go to previous message
icemelid
Messages: 15
Registered: April 2005
Location: BARCELONA
Junior Member

Not much help, dont know where to look at or what to do.

Thanks
Ignasi
Previous Topic: A commit seems to do nothing.
Next Topic: java.sql.SQLException: Exhausted Resultset
Goto Forum:
  


Current Time: Fri Nov 22 05:04:08 CST 2024