How to delete folders that are older than some days using camel

Viewed 1062

I wanted to poll a directory and delete the files and folders that are older than a specified time. I wrote the following code :-

            from("file:input?filterFile=${date:file:yyyyMMddHHmm} <= ${date:now-240h:yyyyMMddHHmm}&delay=60000&delete=true")
            .log("The headers are ${headers}");

It works fine for files and the old files are deleted but this does not work on folders. Can anyone suggest a way to make it work on folders also?

3 Answers

For only for sub directories you can use you can try http://www.instanceofjava.com/2018/02/java-8-delete-folder-and-subfolders.html or something similar to

File directory = new File(dirWay);
if(directory.exists()){

File[] listFiles = directory.listFiles();           
long purgeTime = System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);

for(File listFile : listFiles) {
    if(listFile.lastModified() < purgeTime) {
      FileUtils.forceDelete(new File(listFile.getPath())); // or your way to delete
    }
  }
}

public static void forceDelete(File file) throws IOException

Deletes a file. If file is a directory, delete it and all sub-directories. https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#forceDelete(java.io.File)

You can write a processor to write your logic in java as Apache camel doesn't work on directories, you can try something like below.

from("timer://myTimer?period=2000")
.process(new Processor() {

    public void process(Exchange exchange)
            throws Exception {
        System.out.println("Deleting Directories");
        File directory = new File("D:\\basedirectory");
        removeDirectory(1, directory);
    }

    public void removeDirectory(int daysBack, File directory) {
        if(directory.exists()){
            final File[] listFiles = directory.listFiles();          
            final long purgeTime = 
                System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);

            for(File listFile : listFiles) {
                if(listFile.lastModified() < purgeTime) {
                    System.out.println(listFile.getName() + " - deleted");
                    listFile.delete();
                }
            }
        } 
    }
})
.end();

Draft decision, as example,java decision

File directory = new File(dirWay);
if(directory.exists()){

File[] listFiles = directory.listFiles();           
long purgeTime = System.currentTimeMillis() - (daysBack * 24 * 60 * 60 * 1000);// how old

int  i = listFiles.length //for checking if directory is empty // you can use your logic for checking directory is empty or not

for(File listFile : listFiles) {
    if(listFile.lastModified() < purgeTime) {
      if(listFile.delete()) {
        i--;  //for checking if directory is empty // you can use your logic for checking directory is empty or not   
      }

    }
  }
}

if (i == 0) { // check if directory empty delete it. Because of all files older than 10yers then directory older too. // you can use your logic for checking directory is empty or not
  FileUtils.forceDelete(new File(dirWay));
}

public static void forceDelete(File file) throws IOException

Deletes a file. If file is a directory, delete it and all sub-directories. https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html#forceDelete(java.io.File)

Related