Team LiB
Previous Section Next Section

Searching a Directory for a File that Contains a Regex Expression

In principle, searching a directory for a file that matches a particular pattern is fairly easy. I already have a mechanism in place to search a file, so all I have to do is search a list of files. As you can see in Figure 5-3, the algorithm is recursive.

Click To expand
Figure 5-3: Recursive directory search

Listing 5-13 implements this design by taking advantage of the existing frame-works. It searches through subdirectories, looking for files that happen to contain the regex pattern I described.

Listing 5-13: Search Current and Subdirectories for File Containing the Pattern
Start example
01   /**
02   * Searches through the given directory and finds
03   * the specified files, based the searchPattern that describes
04   * its content. Returns matching files in an ArrayList. This
05   * method searches recursively through the file system.
06   * @param the File currentFile the directory, or file, to start
07   * searching in
08   * @param the String fileExtension, if any, of the file
09   * @param the String searchPattern, the regex that describes
10   * the file content we're looking for
11   * @param the int flags and flags we want to apply to the regex
12   * pattern.
13   * @throws IOException if there's an IO problem
14   *
15   * @return ArrayList containing <code>File</code> objects,
16   * or an empty ArrayList, if no matches are found
17   */
18   public static ArrayList searchDirs(
19       File currentFile,
20       String fileExtension,
21       String searchPattern,
22       int flags
23   ) throws IOException
24   {
25      ArrayList retval = new ArrayList();

26      if (!currentFile.isDirectory())
27      {
28          Map tmp = searchFile(
29              currentFile.getPath(),
30             searchPattern,flags);

31          //if anything was found, add the file
32          if (tmp.size() > 0)
33          {
34               retval.add(currentFile);
35               this.log.finest("added " + currentFile);
36          }
37     }
38     else
39     {   //step through subdirectories
40         File subs[] =
41           currentFile.listFiles(
42               newLocalFileFilter(fileExtension));

43         if (subs != null)
44         {
45           //if the recursive search found anything, add it
46           for (int i=0; i < subs.length; i++)
47           {
48               ArrayList tmp=null;
49               tmp =searchDirs(
50                   subs[i],
51                   fileExtension,
52                   searchPattern,
53                   flags);

54               if (tmp.size() > 0)
55               {
56                  log.info(subs[i].getPath());
57                  retval.addAll(tmp);
58               }
59           }
60         }
61     }

62        return retval;
63   }
64  /**
65  * private filtering class, so that file
66  * searches can be more efficient
67  */
68  private static class LocalFileFilter implements FileFilter{
69      private String extension;
70      LocalFileFilter()
71      {
72          this(null);
73      }

74      LocalFileFilter(String extension)
75      {
76          this.extension = extension;
77      }
78     /**
79     * true if the current file meets the criteria
80     * @param the file pathname to check
81     *
82     * @return true if the file has the extension, or
83     * equals null, or the file is a directory.
84     * Else, returns false.
85     */
86     public boolean accept(File pathname){

87        boolean retval = false;
88        if (extension == null)
89        {
90            retval = true;
91        }
92        else
93        {
94            String tmp = pathname.getPath();
95            if (tmp.endsWith(extension)) retval = true;
96            if (pathname.isDirectory()) retval = true;
97         }

98        return retval;
99     }
100 }
End example

Team LiB
Previous Section Next Section