Sidebar

How do I get a list of files in a directory and sort them by modified date?

0 votes
952 views
asked Jan 7, 2022 by jon-t-7005 (7,590 points)

1 Answer

0 votes

The below code will read files from a directory into an array, then sort the array.

 

// Read a directory, list the files, and rearrange the array
// so the files are in ascending order by modified date
// Use LASTMODIFIED_REVERSE to reverse the order

// Get the source directory
var srcDirectory = new java.io.File("C:\\HL7\\In");

// Create a list of files in an array
var listFiles = srcDirectory.listFiles();

// Sort the array
java.util.Arrays.sort(listFiles, org.apache.commons.io.comparator.LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);

// List the files in the array
for (var i = 0; i < listFiles.length; i++) {
   qie.debug(listFiles[i].getName());
}

answered Jan 7, 2022 by jon-t-7005 (7,590 points)
...