Hi, I’m curious how Processing decides the order of files in a directory listing and if there is a way to sort alphabetical.
I’m using the sample code below and the listing doesn’t seem to be alphabetical or by date modified.
I need to loop through a few thousand images that are in subfolders, the subfolders are alphabetical and I want to keep my list that way.
Any ideas?
import java.util.Date;
void setup() {
// Using just the path of this sketch to demonstrate,
// but you can list any directory you like.
String path = "/Users/philspitler/Desktop/";
println("\nListing info about all files in a directory and all subdirectories: ");
ArrayList<File> allFiles = listFilesRecursive(path);
for (File f : allFiles) {
println("Full path: " + f.getAbsolutePath());
}
noLoop();
}
// Nothing is drawn in this program and the draw() doesn't loop because
// of the noLoop() in setup()
void draw() {
}
// Function to get a list of all files in a directory and all subdirectories
ArrayList<File> listFilesRecursive(String dir) {
ArrayList<File> fileList = new ArrayList<File>();
recurseDir(fileList, dir);
return fileList;
}
// Recursive function to traverse subdirectories
void recurseDir(ArrayList<File> a, String dir) {
File file = new File(dir);
if (file.isDirectory()) {
// If you want to include directories in the list
a.add(file);
File[] subfiles = file.listFiles();
for (int i = 0; i < subfiles.length; i++) {
// Call this function on all files in this directory
recurseDir(a, subfiles[i].getAbsolutePath());
}
} else {
a.add(file);
}
}