We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › counting files of a specific type in a directory
Page Index Toggle Pages: 1
counting files of a specific type in a directory (Read 764 times)
counting files of a specific type in a directory
Jul 27th, 2006, 8:46am
 
I have an application that is grabbing images from a video camera and dumping them in a directory. I needed a way to count how many images were in the folder as the program went along. If you ever needed such a function, here is code that does it (just change the path and the file suffix to whatever is useful to you):

void setup() {

 size (250, 250);

    File imageDir = new File("/Users/mccoy/Pictures/processing/set1/");

    FilenameFilter ff = new FilenameFilter() {
      public boolean accept(File imageDir, String name) {
         return name.endsWith(".bmp");
          }
        };

     String[] theList = imageDir.list(ff);
     int bmpCount = theList.length;
     println(str(bmpCount) + " image files");
}
counting files of any type in a directory
Reply #1 - Jul 27th, 2006, 9:15am
 
If you need to count the total number of files regardless of filetype, the code is even easier:

 File theDir = new File("/Users/mccoy/Sites/");
 String[] theList = theDir.list();
 int fileCount = theList.length;
 println(str(fileCount) + " files");

the File data type and the list() method are part of java.io.File class. Everything under java.io.* is available in processing (I think...?)
Re: counting files of a specific type in a directo
Reply #2 - Jan 24th, 2007, 3:30am
 
Thanks for sharing this code...it's exactly what I was looking for!
A
Page Index Toggle Pages: 1