0

Am new to Java and seeking your help here.

I have a Parent Folder/Directory Named as : DP_E2E_POC Under the Parent Folder i have many sub Folders, i need to find the latest Folder Name under the Parent Folder.

Example: enter image description here

from the image attached i need to fetch the latest folder which is "DELTA_DP_E2E_POC_ManualBuild_20161128.1"

Thanks in Advance, Satish D

Satish Dhanaraj
  • 63
  • 1
  • 4
  • 14

1 Answers1

6

Use the last modified to sort the directories and get the latest one

    File dir = new File("path");
    File[] files = dir.listFiles();
    File lastModified = Arrays.stream(files).filter(File::isDirectory).max(Comparator.comparing(File::lastModified)).orElse(null);
    System.out.println(lastModified);

using for loop

    File dir = new File("/path");
    File max = null;
    for (File file : dir.listFiles()) {
        if (file.isDirectory() && (max == null || max.lastModified() < file.lastModified())) {
            max = file;
        }
    }
Saravana
  • 12,647
  • 2
  • 39
  • 57