简体   繁体   中英

How to recursively list all .rar files in a directory

I want to list the .rar files in a folder and it subfolders till no folders are left.

This is the code I am currently using:

private String getFileNums(File file) {
        if (file.isDirectory()) {
            File[] list = file.listFiles(new FilenameFilter() {
                @Override
                public boolean accept(File file, String s) {
                    return s.toLowerCase().endsWith(".rar");
                }
            });
            int count = 0;
            if (list != null) {
                for (File f : list) {
                    if (f.isDirectory()) {
                        getFileNums(f);
                    } else {
                        count++;
                    }
                }
            }
            return  "Number of rar files in this folder and subfolders is " + count;
        } else {
            long length = file.length();
            return "File size in bytes is " + length;);
        }
    }

The problem I have with this code is that it doesn't list the .rar files in the subfolders, it only list those in the immediate folder.

For example say House is a folder that contains Bedroom (folder), Kitchen (folder), chair.rar (rar file) and door.rar (rar file). The above code only list chair.rar and door.rar , it doesn't list the rar files in Bedroom and Kitchen folders.

Please is there any where this can be fixed?

The problem is with your FilenameFilter . It's filtering out directories whose name does not end with ".rar" as well, which most directories don't. You need to make sure they pass the filter.

 new FilenameFilter() {
     @Override
     public boolean accept(File dir, String filename) {
         // create a file object to check whether we are looking at a directory.
         File f = new File(dir, filename); 
         return f.isDirectory() || filename.toLowerCase().endsWith(".rar");
     }
 }

After reading your comment, I realized that your code fails to "connect" the recursion.

int count = 0;

if (f.isDirectory()) {
    // Nothing comes out of the recursive call.
    // The result is completely lost
    getFileNums(f);
}

Also, you want to return int instead of String for a method that count the number of RAR files recursively.

private int getFileNums(File file) {
    if (file.isDirectory()) {
        File [] files = file.listFiles();
        if (files != null) {
            int count = 0;
            for (File f : files) {
                 if (f.isDirectory()) {
                     count += getFileNums(f);
                 } else if (f.getName().toLowerCase().endswith(".rar")) {
                     count++;
                 }
            }
            return count;
        } else {
            return 0;
        }
    } else {
        throw new IllegalArgumentException("Expecting a directory.");
    }
}

If you are using Java 8

You can use Files.walk .

import java.nio.file.*;

long numOfRar = Files.walk(file.toPath(), FileVisitOption.FOLLOW_LINKS)
                     .filter(p->p.toString().toLowerCase().endsWith(".rar"))
                     .count();

Sometimes it is easier to see things in Pseudocode. I think so, anyway.

processDirectory(thisDir)
  for each entry in thisDir
    if (entry is directory)
      processDirectory(entry)  // Recurse.
    else if (entry is .rar file)
      processRarFile(entry)
    endif
  endfor
end

Your code does not match that pattern, as Luke pointed out.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM