Processing: finding images in a directory listing

Finding which the images are, in a particular directory on a filesystem, can be a little tricky. So I’ve put together some code which helps with this.

The code here is in two parts:

–  The listFileNames() function, takes a directory url, and returns a String[] array of filenames/urls in the directory.

– The findImgFiles() function can be used to take the file listing output of listFileNames() and return only the files which are image files.

findImgFiles() is nothing particularly complex, the function takes a list of filenames or url’s, checks the file type suffix if the file is an image file. A String[] array of valid image file names / urls is returned.
Currently it only checks for jpeg, jpg, tif, tiff, png files. However, if you want to check for other image file suffixes, it’s easy to modify. Just check the relevant String[] array of file suffixes in the code below.

( yeah, sorry about the lack of proper indentation below, my html skills are from the last century )


// This function returns all the files in a directory as an array of Strings  String[] listFileNames(String dir) {

File file = new File(dir);
if (file.isDirectory()) {
String names[] = file.list();
return names;
else {
// If it’s not a directory
return null;



String[] findImgFiles( String[] filenames ) {

// this is where we’ll put the found image files
String[] outList_of_foundImageFiles = {

// to find out what a valid image file suffic might be
String[] list_of_imageFileSuffixes = {
“jpeg”, “jpg”, “tif”, “tiff”, “png”

// ___ get the dir path in good shape
// – is there a slash at the end of the dir path???!?!?
// – check whether there’s a slash at the end of the suggested path to the images dir
// and add one if it’s not there.
// (it’ll make constructing complete paths to the images easier
if( images_dir_path.charAt( images_dir_path.length() -1 ) == ‘/’ ) {
println(” looks like there’s a slash at the end of the dir path… no need for modifications “);
else {
images_dir_path = images_dir_path+’/’ ;
println(” aha! it’s missing a slash at the end, let’s add one. \n\t images_dir_path is now = “+images_dir_path );

// ____ go through all the filenames
// and check whether the fileending is not one for images
for( int file_i = 0; file_i < filenames.length ; file_i++ ) {
// for( int file_i = 0; file_i < 10 ; file_i++ ) {

println(” looking at file “+filenames[file_i]+” checking if it might not just be a image file “);

String[] curr_filenameSplit = splitTokens( filenames[ file_i], “.” );

// ___ now check whether file suffix matches any in
// our little list of filesuffixes
for( int fileSuffix_i = 0 ; fileSuffix_i < list_of_imageFileSuffixes.length ; fileSuffix_i++ ) { // only do this is the file has a suffix!! // (i.e. which it hopefully has if a split string results // in more than one part/length ) if( curr_filenameSplit.length > 1 ) {
// fetch the filesuffixes as strings
// (this might be a long-winded way of doing it,
// but it takes out some the common instances of bugs… )
String examinedFile_filesuffix = curr_filenameSplit[1] ;
String listOfValid_fileSuffixed = list_of_imageFileSuffixes[fileSuffix_i] ;

// do the actual comparison
if( examinedFile_filesuffix.equals( listOfValid_fileSuffixed ) ) {
// and if it’s a matching image file suffix, add the whole
// filepath to the file to the list out outfilenames
outList_of_foundImageFiles = append( outList_of_foundImageFiles, images_dir_path+filenames[ file_i ] );

// and return something nice
return outList_of_foundImageFiles ;

This entry was posted in Uncategorized. Bookmark the permalink.