some rearrangement
// https://discourse.processing.org/t/file-listing-order/7148
// @philspitler @Architector_4
ArrayList<File> myfiles;
String path = "/home/pi/projects/processing/sketchbook";
boolean piconly = true;
void setup() {
println("\nListing info about all files in a directory and all subdirectories \n _filter_ piconly: "+piconly);
myfiles = listFilesRecursive(path,piconly);
for (File afile : myfiles) println("File: "+afile ); // afile.getAbsolutePath());
}
void draw() {
}
// Function to get a list of all files in a directory and all subdirectories // with piconly filter
ArrayList<File> listFilesRecursive(String dir,boolean piconly) {
ArrayList<File> fileList = new ArrayList<File>();
recurseDir(fileList, dir, piconly); // ( file array , root_directory , pictures only )
return fileList;
}
// Recursive function to traverse subdirectories
void recurseDir(ArrayList<File> a, String dir, boolean piconly) {
boolean showdirs = false;
File file = new File(dir);
if (file.isDirectory()) {
if ( showdirs ) a.add(file); // only if you want to include directories in the list
File[] subfiles = file.listFiles();
java.util.Arrays.sort(subfiles); //Sorts all files by name
for (int i = 0; i < subfiles.length; i++) recurseDir(a, subfiles[i].getAbsolutePath(), piconly ); // Call this function on all files in this directory
} else {
String fileName = file.getName();
if ( piconly ) {
if ( //fileName.endsWith(".other-random-formats")||
fileName.endsWith(".gif") ||
fileName.endsWith(".jpg") ||
fileName.endsWith(".jpeg") ||
fileName.endsWith(".tga") ||
fileName.endsWith(".png") ||
fileName.endsWith(".svg")
) a.add(file); // if picture for piconly
} else { a.add(file); } // all
}
}