Home » Developer & Programmer » JDeveloper, Java & XML » newbie Getting a date of a text file
newbie Getting a date of a text file [message #264076] Fri, 31 August 2007 12:03 Go to next message
shall42
Messages: 10
Registered: October 2006
Junior Member
I'm trying to get the date for a text file.
Actually I want to get just the *.txt files and delete them
that are older than 2 days.

My problem is getting the date for the text file.
Below is my code:
The loop stops at the line .lastModified();
=========================================
create or replace and compile java source named "DirList"
as
import java.io.*;
import java.sql.*;
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class DirList
{

public static void getList(String directory)
throws SQLException {
try {
File myDir = new File("D:/devapps/dirrigweb/IrrigPDF/");
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES ('beginning') };
File [] myList = myDir.listFiles();
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES ('before LOOP') };
for (int i = 0; i < myList.length; i++) {
String myName = myList[i].getName();
String strFile="D:/devapps/dirrigweb/IrrigPDF/"+myName;
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES (:myName) };
long myDate=myList[i].lastModified();
#sql { INSERT INTO DIR_LIST (FILENAME)
VALUES ('Past date') };
}

} catch (Exception e) {
e.printStackTrace();
}
}

}
/
=========================
Suggestions?
TIA
Steve
Re: newbie Getting a date of a text file [message #264145 is a reply to message #264076] Sat, 01 September 2007 02:00 Go to previous messageGo to next message
Frank
Messages: 7901
Registered: March 2000
Senior Member
Can you define 'the loop stops'?
What happens? Are he debug messages saved? What files are in the directory?
Re: newbie Getting a date of a text file [message #264163 is a reply to message #264076] Sat, 01 September 2007 05:42 Go to previous message
psix666
Messages: 51
Registered: April 2007
Location: Azerbaijan
Member

Try this:

public static String ShowFiles(String pDirName, String pExtension)
throws  IOException, SQLException {
    int vFileNum = 0;
    if (pDirName.trim() == null){ //direktoriya ne zadana
        return "Error: Directory name must be specified";
    } else
    if (pExtension.trim() == null){  // raswireniye ne zadano
        return "Error: File extension must be specified";
    } else {
        SimpleDateFormat vDateFormater = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat vTimeFormater = new SimpleDateFormat("HHmmss");
        try{
            File directory = new File(pDirName);
            File[] list = directory.listFiles();
            if (pExtension.toLowerCase().equals("*.*")) {
                for (int i=0; i<list.length; i++){
                    String file_name = list[i].getName();
                    vFileNum=vFileNum+1;
                    double file_size = list[i].length();
                    String file_create_date = vDateFormater.format(new java.sql.Date(list[i].lastModified()));
                    String      create_time = vTimeFormater.format(new java.sql.Time(list[i].lastModified()));
                    #sql{insert into f10files
                         ( dir_name,  file_name,  file_size,  file_create_date,  create_time)  values
                         (:pDirName, :file_name, :file_size, :file_create_date, :create_time)
                    };
                };
            } else{
                for (int i=0; i<list.length; i++){
                    String file_name = list[i].getName();
                    if (file_name.toLowerCase().endsWith(pExtension.toLowerCase())) {
                        vFileNum=vFileNum+1;
                        double file_size = list[i].length();
                        String file_create_date = vDateFormater.format(new java.sql.Date(list[i].lastModified()));
                        String      create_time = vTimeFormater.format(new java.sql.Time(list[i].lastModified()));
                        #sql{insert into f10files
                             ( dir_name,  file_name,  file_size,  file_create_date,  create_time)  values
                             (:pDirName, :file_name, :file_size, :file_create_date, :create_time)
                        };
                    };
                };
            };
            #sql{commit};
        } catch (Exception ex){
            return "Error: "+ex.toString();
        }
    }
    return "Success: "+vFileNum+" files was successefully imported.";
}
}
Previous Topic: Unknow Problem with JExcelAPI
Next Topic: Java Stored Procs and Oracle 10Gb PATH variable issue
Goto Forum:
  


Current Time: Tue Dec 03 11:53:25 CST 2024