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 & HelpPrograms › How to get the number of files in a folder
Page Index Toggle Pages: 1
How to get the number of files in a folder (Read 1071 times)
How to get the number of files in a folder
May 27th, 2008, 7:03pm
 
Hi, I'm having a lot of trouble trying to solve a problem:

I need to know how many jpg files are in my sketch data folder.

Is there a way to do that ? A function ? a Library ?

Can anyone help me ?

Thanks !
Re: How to get the number of files in a folder
Reply #1 - May 27th, 2008, 8:38pm
 
java.io.File provides a lot of useful methods for such issues :

Quote:
// have a look in the data folder
java.io.File folder = new java.io.File(dataPath(""));

// this is the filter (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
String[] filenames = folder.list(jpgFilter);

// get the number of jpg files
println(filenames.length + " jpg files in specified directory");

// print the filenames
for (int i = 0; i < filenames.length; i++) println(filenames[i]);
Re: How to get the number of files in a folder
Reply #2 - May 28th, 2008, 2:25pm
 
thanks, I'll try that.

Re: How to get the number of files in a folder
Reply #3 - Jun 13th, 2008, 3:27am
 
antiplastik, can you add this to hacks? http://www.processing.org/hacks/
Re: How to get the number of files in a folder
Reply #4 - Jun 13th, 2008, 9:59am
 
Of course, just added it in Using built-in Java classes section:

http://processing.org/hacks/doku.php?id=hacks:listing-files

Page Index Toggle Pages: 1