Listing files

From Processing

Jump to: navigation, search
Versions: 1.0+
Contributors: antiplastik
Started: 2008-06-13


Say, you need to know how many JPG files are in your sketch data folder (or any other file type).

Source Code

/**
listing-files taken from http://wiki.processing.org/index.php?title=Listing_files
@author antiplastik
*/
 
// we'll have a look in the data folder
java.io.File folder = new java.io.File(dataPath(""));
 
// let's set a filter (which returns true if file's extension is .jpg)
java.io.FilenameFilter jpgFilter = new java.io.FilenameFilter() {
  boolean accept(File dir, String name) {
    return name.toLowerCase().endsWith(".jpg");
  }
};
 
// list the files in the data folder, passing the filter as parameter
String[] filenames = folder.list(jpgFilter);
 
// get and display the number of jpg files
println(filenames.length + " jpg files in specified directory");
 
// display the filenames
for (int i = 0; i < filenames.length; i++) {
  println(filenames[i]);
}

Downloads

Related Links

Personal tools