Sidebar

Need to list only files in a given directory without checking if file.isFile()

0 votes
162 views
asked Sep 22, 2021 by pradeepkumar-j-1408 (160 points)
var directory = new java.io.File(directory);

var fileList = directory.listFiles(); - Need help here to use Java.io.FileFilter to list only files

1 Answer

+1 vote

You could try using the FileUtils library.
https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html

You can specify extensions of your files, of which your directories should not be returned.

var srcDirectory = new java.io.File("C:\\myDirectory\\path\\");

// Use this to filter by extensions
//var extensions = StringUtils.splitByWholeSeparator('pdf,json', ',');

//Use null to list all files regardless of extension
var extensions = null;

var FileUtils = org.apache.commons.io.FileUtils;
var listFiles = FileUtils.convertFileCollectionToFileArray(FileUtils.listFiles(srcDirectory, extensions, false));

for (var i = 0; i < listFiles.length; i++) {
   qie.debug(listFiles[i].getName());
}

 

answered Sep 22, 2021 by mike-r-7535 (13,650 points)
edited Sep 22, 2021 by brandon-w-8204
...