listing-files

versions0135+
contributorsantiplastik
started on2008-06-13 00:47

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://processinghacks.com/hacks: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]);

Related Links

More info about java.io.File and java.io.FileFilter can be found in Sun's JavaDoc:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/File.html http://java.sun.com/j2se/1.4.2/docs/api/java/io/FileFilter.html

hacks/listing-files.txt · Last modified: 2008-06-13 00:55 by nikoony