How to delete files in java when find according to older then N days and file type

660

how to delete files in java when we want to use old, and file type in an folder

please give the complete code

  • Java

  • asked 3 years ago
  • Sunny Solu

1Answer


0
public void remove_old_files(String fiile_path) {
        try {
            File directory = new File(fiile_path);
            if (directory.exists()) {
                File[] listFiles = directory.listFiles();
                Date today = new Date();
                long purgeTime1 = today.getTime();
                long backtime = new Long("2592000000");//2592000000
                long purgeTime = (purgeTime1 - backtime);
                for (File listFile : listFiles) {
                    if (listFile.lastModified() < purgeTime) {
                        if (listFile.getName().endsWith(".dat")) {
                            if (!listFile.delete()) {
                                System.err.println("Unable to delete file: " + listFile);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.toString());
        }
    }

Call to function

remove_old_files("d:/datafolder");

  • answered 3 years ago
  • Community  wiki

Your Answer

    Facebook Share        
       
  • asked 3 years ago
  • viewed 660 times
  • active 3 years ago

Hot Questions