Read quantity of specific files on data folder

edited June 2015 in How To...

How could I know how much files are of a specific kind on Processing's data folder or other specific folder?

Such as '.mp4'

Answers

  • Thanks, @GoToLoop! One little thing:

    As for http://forum.processing.org/two/discussion/8233/counting-the-files-in-a-folder#latest

    And if the number of mp4 files before each execution of the program is static, I could use the sample:

    File f = dataFile("folderName");
    String[] names = f.list(FILTER);
    printArray(names);
    

    And after get names.length to be aware of the number? The folder name shoud be "data"? And what would be an example of FILTER?

  • edited June 2015 Answer ✓

    If those ".mp4" files are inside sketch's "/data/" subfolder, and you're only interested about how many there are, the code below goes direct to the point: ;)

    // forum.processing.org/two/discussion/11279/
    // read-quantity-of-specific-files-on-data-folder
    
    import java.io.FilenameFilter;
    
    static final FilenameFilter MP4_FILTER = new FilenameFilter() {
      @ Override boolean accept(File path, String name) {
        return name.toLowerCase().endsWith(".mp4");
      }
    };
    
    void setup() {
      int qty = dataFile("").list(MP4_FILTER).length;
      println(qty);
      exit();
    }
    

    P.S.: If you're sure all ".mp4" extensions are 100% in toLowerCase() already, that is there's no ".MP4", you can safely remove it and gain some minuscule speed: return name.endsWith(".mp4");

  • Thank you, @GoToLoop! Got it all.

Sign In or Register to comment.