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 › Reading the contents of a directory
Page Index Toggle Pages: 1
Reading the contents of a directory? (Read 1134 times)
Reading the contents of a directory?
Dec 24th, 2005, 6:04am
 
One thing I'd like to do on the next project I'm going to work on is read the contents of a directory and load the info into an array.  Really all I need is the names of all the files in a given directory and can just load their names into the array as strings, but I can't seem to find a way in Processing to do this.

I see I can load the contents of a single file into an array, but that doesn't help me any, as writing the entire directory list to a file manually would defeat the purpose (and wouldn't be dynamic).

I feel as though I'm missing something obvious, but I'm not sure.  Anyone have any ideas here?
Re: Reading the contents of a directory?
Reply #1 - Dec 24th, 2005, 9:29am
 
Easily done in Java:

Code:
File [] files=new File("nameofdirectory").listFiles();
for(int i=0; i<files.length; i++) println(files[i]);
Re: Reading the contents of a directory?
Reply #2 - Dec 24th, 2005, 11:19pm
 
Very cool. This sort of thing should belong in the learning section.
Re: Reading the contents of a directory?
Reply #3 - Dec 25th, 2005, 1:12pm
 
Slight amendment: The code above gives you an array of File objects, not the filenames themselves. If you just wanted the names then this would do it:

Code:
File [] files=new File("nameofdirectory").listFiles();
String filenames[]=new String[files.length];
for(int i=0; i<files.length; i++) {
filenames[i]=files[i].getAbsolutePath();
println(filenames[i]);
}
Re: Reading the contents of a directory?
Reply #4 - Dec 27th, 2005, 6:33am
 
I did some research online about the Java for this and realized the file objects, and also saw how to get the names, but it's good you posted it for others.  I appreciate the help, this sets me on a good track.
Reading the contents of a directory online?
Reply #5 - Dec 28th, 2005, 4:55am
 
how would one go about doing this with a folder on the server the applet is hosted on?

thanks

-seltar
Re: Reading the contents of a directory?
Reply #6 - Dec 28th, 2005, 11:16am
 
For security reasons that's not normally possible. I found one link that gives more info:

http://www.rgagnon.com/javadetails/java-0185.html
Re: Reading the contents of a directory?
Reply #7 - Dec 28th, 2005, 9:46pm
 
Thanks, but i solved it using PHP.. read the directory, and caught the information in processing in an array..

thanks again though Smiley
you can see it in action here:
Rings Advanced

-seltar
Re: Reading the contents of a directory?
Reply #8 - Dec 28th, 2005, 10:14pm
 
Using PHP is a good solution, should have thought of that.
Re: Reading the contents of a directory?
Reply #9 - Dec 29th, 2005, 3:25pm
 
yeah, just be careful about how you write the php script for file access.. you don't want folks to be able to list arbitrary directories on your machine via the use of ../ and things like that.
Page Index Toggle Pages: 1