5 Replies Latest reply: Nov 25, 2010 6:26 AM by 800330 RSS

    filenameFilter  regexp for exclude a file

    816754
      Hi,


      I have a little method which clears a directory by deleting all its content. Now I would like to be able to use a regular expression to control what I want to delete. Basically I want to delete all but one single file. For instance: suposse I have a directory with following files:

      foo1.txt
      foo2.ana
      foo.wav
      another.txt
      this_do_not_erase.wav
      another.wav


      and I would like to erase all but the one called "this_do_not_erase.wav". Is it possible to get a regular expression in Java to create a filenameFilter to be used with File.listFiles() method?

      I tried the following expression:

      "!"+ nameOfFile + "\\" wavExt "^.*"

      where nameOfFile variable contains the name of file to be kept in the directory and wavExt=".wav".

      Following is the function that erases all files in the directory:

      private static void clearDirectory(File directory, String regexp) throws SecurityException
      {
      FilenameFilter filter = createFilter(regexp);

      File[] fileList = directory.listFiles(filter);
      for (int fileIdx = 0;fileIdx < fileList.length;fileIdx++)
      fileList[fileIdx].delete();
      }


      The method I wrote to create the filenameFilter is the following:

      public static FilenameFilter createFilter(final String stringToMatch){

      FilenameFilter filter = new FilenameFilter() {

      public boolean accept(File dir, String name) {

      //return name.toLowerCase().contains(stringToMatch.toLowerCase());
      return name.toLowerCase().matches(stringToMatch.toLowerCase());
      }
      };
      return filter;
      }


      Lots of thanks.
        • 1. Re: filenameFilter  regexp for exclude a file
          796440
          new FileNameFilter() {
            public boolean accept(String fileName) {
              return !fileName.matches("(?i)file_to_keep"); // (?i) says ignore case
            }
          }
          • 2. Re: filenameFilter  regexp for exclude a file
            816754
            yes but the problem is that the "file_to_keep"'s name changes so I need to be able to change the name of the file to each iteration of the loop.

            Other solution could be just copy the file with a specific name and then be able to use what you say, but I would like to keep the function as flexible as posible. If there is no other way, I'll end up doing what you say.


            Anyway lots of thanks. :)
            • 3. Re: filenameFilter  regexp for exclude a file
              aksarben
              I've done similar things in other contexts (e.g., excluding files when publishing Web site changes via FTP). If the file patterns to exclude might change/grow from time to time, you could keep their regex patterns in a properties file which your FileFilter implementation reads.
              • 4. Re: filenameFilter  regexp for exclude a file
                796440
                813751 wrote:
                yes but the problem is that the "file_to_keep"'s name changes so I need to be able to change the name of the file to each iteration of the loop.
                That's why Gosling invented variables. A regex is just a String. It doesn't have to be a literal.
                Other solution could be just copy the file with a specific name and then be able to use what you say, but I would like to keep the function as flexible as posible.
                No idea what you're saying here.
                • 5. Re: filenameFilter  regexp for exclude a file
                  800330
                  From the description of wanting to exclude one specific file and delete the rest, I don't see why you'd need a regexp. That requirement would be solved by a filter that accepts all but a filename equal to the to be excluded file.

                  But your file name filter can be a real class with all your desired functionality coded in, for example:
                       public  class RegexpExcludingOneFileNameFilter implements FilenameFilter {
                            private final String regexp;
                            private final String excludeName;
                            
                            public RegexpExcludingOneFileNameFilter(String regexp, String fileToExclude) {
                                 this.regexp = regexp;
                                 excludeName = fileToExclude;
                            }
                            @Override
                            public boolean accept(File dir, String name) {
                                 return name.matches(regexp) && !name.equals(excludeName);
                            }
                       }
                  You said somewhere that you want to change the fileToExclude every iteration, which to me means every pass through the loop and seems odd. But constructng a new filter each invocation of your clear method with the changing name of the file to keep looks straight forward to me...