Java Directory to File List getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern)

Here you can find the source of getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern)

Description

Returns the set of all files in the given directory, matching the given pattern (ignoring case)

License

Open Source License

Parameter

Parameter Description
sDirectoryPath The directory path
sPattern The pattern to match against <p>

Return

The required list of files

Declaration

private static String[] getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.io.File;

import java.util.Vector;

public class Main {
    /**//from   w ww  .j a  va2 s.  c  o  m
     * Returns the set of all files in the given directory, matching the given
     * pattern (ignoring case)
     * <p>
     * 
     * @param sDirectoryPath
     *            The directory path
     * @param sPattern
     *            The pattern to match against
     *            <p>
     * @return The required list of files
     */
    private static String[] getAllFilesMatchingThisPatternIgnoreCase(String sDirectoryPath, String sPattern) {

        String[] asAllFiles = getAllFiles(sDirectoryPath);

        Vector vsFiltered = new Vector();
        for (int i = 0; i < asAllFiles.length; i++) {
            if (asAllFiles[i].toLowerCase().matches(sPattern.toLowerCase())) {
                vsFiltered.add(asAllFiles[i]);
            }
        }

        String[] asFilteredFiles = new String[vsFiltered.size()];
        for (int i = 0; i < vsFiltered.size(); i++) {
            asFilteredFiles[i] = vsFiltered.elementAt(i).toString();
        }

        return asFilteredFiles;
    }

    /**
     * Returns the set of all files in the given directory
     * <p>
     * 
     * @param sDirectoryPath
     *            The directory path
     *            <p>
     * @return The required list of files
     */
    private static String[] getAllFiles(String sDirectoryPath) {

        if (sDirectoryPath == null) {
            return new String[0];
        }

        File fileThisDirectory = new File(sDirectoryPath);

        if (fileThisDirectory.isDirectory() == false) {
            return new String[0];
        } else {
            String[] asFileList = fileThisDirectory.list();
            if (asFileList == null || asFileList.length == 0) {
                return asFileList;
            }
            return asFileList;
        }
    }
}

Related

  1. getAllFilesInHierarchy(final String basePath, final FilenameFilter filter)
  2. getAllFilesInSubFolder(String base, String ending)
  3. getAllFilesInternal(final File aPath, final FilenameFilter filter, final List fileList)
  4. getAllFilesLeastRecentFirst(File directory)
  5. getAllFilesMatching(File srcDir, final String regex)
  6. getAllFilesPresentInFolder(File srcPath)
  7. getAllFilesRecursively(File dir, List filelist)
  8. getAllFilesRecursively(File directory, Collection files)
  9. getAllFilesWithExtension(String directory, final String extension)