From a6c80364da4e67df347f02b783511b1aaea9df3c Mon Sep 17 00:00:00 2001 From: Cristian Maglie Date: Mon, 9 Dec 2013 00:10:36 +0100 Subject: [PATCH] FileUtils: added function to recursively find files with given extension --- app/src/processing/app/helpers/FileUtils.java | 68 +++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/app/src/processing/app/helpers/FileUtils.java b/app/src/processing/app/helpers/FileUtils.java index 592d2dc74..e12fd1fbb 100644 --- a/app/src/processing/app/helpers/FileUtils.java +++ b/app/src/processing/app/helpers/FileUtils.java @@ -1,6 +1,7 @@ package processing.app.helpers; import java.io.*; +import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Random; @@ -188,4 +189,71 @@ public class FileUtils { } } } + + /** + * Returns true if the given file has any of the given extensions. + * @param file + * File whose name to look at + * @param extensions + * Extensions to consider (just the extension, without the + * dot). Should all be lowercase, case insensitive matching + * is used. + */ + public static boolean hasExtension(File file, String... extensions) { + return hasExtension(file, Arrays.asList(extensions)); + } + + public static boolean hasExtension(File file, List extensions) { + String pieces[] = file.getName().split("\\."); + if (pieces.length < 2) + return false; + + String extension = pieces[pieces.length - 1]; + + return extensions.contains(extension.toLowerCase()); + + } + + /** + * Recursively find all files in a folder with the specified + * extension. Excludes hidden files and folders and + * source control folders. + * + * @param folder + * Folder to look into + * @param recursive + * true will recursively find all files in sub-folders + * @param extensions + * A list of file extensions to search (just the extension, + * without the dot). Should all be lowercase, case + * insensitive matching is used. If no extensions are + * passed, all files are returned. + * @return + */ + public static List listFiles(File folder, boolean recursive, + String... extensions) { + return listFiles(folder, recursive, Arrays.asList(extensions)); + } + + public static List listFiles(File folder, boolean recursive, + List extensions) { + List result = new ArrayList(); + + for (File file : folder.listFiles()) { + if (isSCCSOrHiddenFile(file)) + continue; + + if (file.isDirectory()) { + if (recursive) + result.addAll(listFiles(file, true, extensions)); + continue; + } + + if (extensions.isEmpty() || hasExtension(file, extensions)) + result.add(file); + } + return result; + } + + }