1

I have tried with how to retain files based on n number of days. This is the logic to do so.

public static void purgeFiles(int daysToRetain, String directory){

    File backupDir = new File(directory);
    long purgeTime = System.currentTimeMillis() - (daysToRetain * 24L * 60L * 60L * 1000L);
    if(backupDir.exists()){
        File[] listOfBackupFiles = backupDir.listFiles();
        for(File backupFile : listOfBackupFiles){
            if(backupFile.lastModified() < purgeTime){
                if(backupFile.isFile()){
                    System.out.println(backupFile.getName()+" is going to delete");
                    backupFile.delete();
                }
        }

    }

}

}    

Here I have a doubt that how to retain latest 10 files in a directory. File creation date and times are different.

There is no fixed time to create files. Files will not get create daily or weekly or monthly. One file will get create in 2 days, others will be in 5 days.

So, I want to retain latest 10 files.

Mateusz Sroka
  • 2,255
  • 2
  • 17
  • 19
JSP
  • 447
  • 2
  • 8
  • 21

1 Answers1

2

Best way to list files in Java, sorted by Date Modified?

You can use a Comparator to order your array by date modified. Then, just check if the array is bigger then 10, and execute your code for all items after 10.

Example:

import java.io.File;
    import java.util.Arrays;
    import java.util.Comparator;

    public class PurgeFiles
    {

        public static void main(String[] args)
        {
            purgeFiles(10, 10, "C:/testdir/");
        }

        public static void purgeFiles(int daysToRetain, int filesToRetain, String directory)
        {

            File backupDir = new File(directory);
            long purgeTime = System.currentTimeMillis() - (daysToRetain * 24L * 60L * 60L * 1000L);
            if (backupDir.exists())
            {
                File[] listOfBackupFiles = backupDir.listFiles();

                Arrays.sort(listOfBackupFiles, new Comparator<File>()
                {
                    public int compare(File f1, File f2)
                    {
                        return Long.valueOf(f2.lastModified()).compareTo(f1.lastModified());
                    }
                });

                if(listOfBackupFiles.length > filesToRetain){
                    File[] listOfBackupFilesToCheckForDeletion = new File[listOfBackupFiles.length - filesToRetain];
                    System.arraycopy(listOfBackupFiles, filesToRetain, listOfBackupFilesToCheckForDeletion, 0, listOfBackupFiles.length - filesToRetain);

                    for (File backupFile : listOfBackupFilesToCheckForDeletion)
                    {
                        if (backupFile.lastModified() < purgeTime)
                        {
                            if (backupFile.isFile())
                            {
                                System.out.println(backupFile.getName() + ", " + backupFile.lastModified() + " is going to delete");
                                backupFile.delete();
                            }
                        }
                    }
                }
            }
        }
    }
Community
  • 1
  • 1
JohannisK
  • 535
  • 2
  • 10
  • Johannisk, thanks for your prompt response.Its worked. – JSP Apr 24 '15 at 14:57
  • No problem, I'm not sure if the System.arraycopy is the ideal way to skip the first 10 files. If there's a better way please correct me. – JohannisK Apr 24 '15 at 15:00