问题
I am trying to get the timestamp of a text file abc.txt in some directory XYZ in oracle server. This file can get updated at any time in the day and i have to check if the file was updated any time after yesterday midnight, if yes i need to email that file as an attachment.
Is there any other way i can check this?
I have searched a lot over internet but could not find the solution. Seriously not getting a clue of how to get it done.
It would be great if anyone could guide me.
Thanks.
回答1:
I think you will have to do this by writing a java procedure, as described here by Tom Kyte:
http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:439619916584
GRANT JAVAUSERPRIV to <your user>
/
create global temporary table DIR_LIST
( filename varchar2(255) )
on commit delete rows
/
create or replace and compile java source named "DirList"
as
import java.io.*;
import java.sql.*;
public class DirList
{
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 DIR_LIST (FILENAME)
VALUES (:element) };
}
}
}
/
create or replace procedure get_dir_list( p_directory in varchar2 )
as language java
name 'DirList.getList( java.lang.String )';
/
回答2:
Another approach might be to make use of the preprocessor directive for external tables. Please have a look at Mr. Kyte's article in Nov/Dec 2012 Oracle Magazine. He is playing with unix df, you can play with unix ls or windows dir.
http://www.oracle.com/technetwork/issue-archive/2012/12-nov/o62asktom-1867739.html
SQL> create table df
2 (
3 fsname varchar2(100),
4 blocks number,
5 used number,
6 avail number,
7 capacity varchar2(10),
8 mount varchar2(100)
9 )
10 organization external
11 (
12 type oracle_loader
13 default directory exec_dir
14 access parameters
15 (
16 records delimited
17 by newline
18 preprocessor
19 exec_dir:'run_df.sh'
20 skip 1
21 fields terminated by
22 whitespace ldrtrim
23 )
24 location
25 (
26 exec_dir:'run_df.sh'
27 )
28 )
29 /
Table created.
来源:https://stackoverflow.com/questions/17338574/getting-the-timestamp-of-a-file-using-pl-sql