Sunday 23 March 2014

Delete particular folder base don Time Criteria

package Bean;

import java.io.File;
import java.util.Calendar;

public class DeleteDir {

    private static void autoDeleteFileAndDir(String filePath, Integer hours,
            Integer minutes) {
        Long timeDiff;
        if (filePath != null && !filePath.isEmpty()
                && (hours != null || minutes != null)) {
            if(hours!=null&&hours>0){
                timeDiff= (long) (hours*60 * 60 * 1000);
            }else {
                timeDiff= (long) (minutes * 60 * 1000);
            }
           
            File file = new File(filePath);

            System.out
            .println("Now will search folders and delete files in :: ,\nFile path :: "
                    + file.getAbsolutePath());
           
            if (file.isDirectory()) {
                for (File f : file.listFiles()) {
                    if (f.isDirectory()) {
                        Long FileTimeDiff = (Calendar.getInstance().getTime().getTime() - f
                                .lastModified());
                       
                        System.out.println("\n\nf.lastModified() :: "
                                + f.lastModified()
                                + "\nnew Date().getDate() :: "
                                + Calendar.getInstance().getTime().getTime() + "\nDiff :: "
                                + FileTimeDiff);
                       
                        if (FileTimeDiff > timeDiff) {
                            if (f.delete()) {
                       
                                System.out.println("\n\n" + f.getName()
                                        + ":: Dir deleted!!!!!");
                               
                            }
                        } else {
                           
                            System.out.println("\n\n" + f.getName()
                                    + ":: Dir not deleted!!!!!");
                           
                        }
                    }
                }
            }
           
        }else {
            System.out.println("Invalid parameter");
        }
       
    }

No comments: