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" );


  // 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)) {
            //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]);
            System.out.println("Usage: Delete [directory to scan]");

Recommended Answers

All 3 Replies

>> String l = Doc +"\\"+ file;

The problem is with this line. Windows uses backslash (\) as the file separator whereas *nix uses forward slash (/) as the file separator. The simplest solution here would be to *not* manually create file names/path but use a method which automatically gives you a list of File objects in a directory.

File dir = new File(baseDirPath);
for(File f : dir.listFiles()) {
  if(!f.delete()) {
    System.out.println("Failed to delete file: " + f);

and to always use the system properties to retrieve operating system specific things like that if you have to.

Also you can use ' String l = Doc + File.separator + file; ' instead of ' String l = Doc +"\\"+ file; '.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.