Hi guys;
Am stuck on the following piece of code which is meant to
delete obsolete files in a directory in an sms server.
It runs well from a windows platform on which it was coded in, but can not run from a linux platforrm correctly.
On the linux lastmodified date of a file is given in epoch time yet the file was created a day ago and the file is never deleted when the delete condtion is met.
Any assistancer will be highly appreciated
package Delete;
import java.io.File;
import java.util.Date;
import static java.lang.System.out;
/**
* @author Jeffa & Mutua
*/
public class Delete {
public Delete(String Doc)
{
System.out.println("Directory scanning: "+Doc);
File f = new File(Doc);
if (!f.exists() )
{
out.println( "Directory doesnt exists" );
System.exit(1);
}
// get a list of all files in a directory
File dir = new File(Doc);
String[] files = dir.list();
out.println( "Files in this directory are:" );
for ( String file : files ){
out.println( file );
String l = Doc +"\\"+ file;
File g = new File(l);
if(g.isDirectory() && g.exists()){
new Delete(g.getAbsolutePath());
}
// when was this file last modified?
out.println( "last modified: " + new Date( g.lastModified() ) );
long Time ;
Time =(System.currentTimeMillis() - g.lastModified());
out.println("Age of the file is: " + (Time/86400000) + " days" );
// Attempt to delete it
//86400000 ms is equivalent to one day
if (Time > (86400000 * 2)) {
g.delete();
//final boolean Success = g.exists() && g.delete();
out.println("File Deleted");
}
else {
out.println("File not deleted");
}
}//end of for loop
}
public static void main(String[] args)
{
if(args != null && args.length > 0 && args[0] != null && args[0].length() > 3)
{
System.out.println("Directory to scan: "+args[0]);
new Delete(args[0]);
}
else
{
System.out.println("Usage: Delete [directory to scan]");
}
}
}