jayjaypg22
jayjaypg22

Reputation: 1751

Finder for folder name in a given path

I use java 1.7 and want to find every folder with a name beginning with "modRepart" in a given path. I 've found code to find files but not to find folders. I also find java 1.8 code that I can't use.

Upvotes: 0

Views: 110

Answers (2)

achAmháin
achAmháin

Reputation: 4266

You could modify this existing answer, and just add in a startsWith clause:

File file = new File("C:\\path\\to\\wherever\\");
        String[] names = file.list();

        for (String name : names) {
            if (new File(file + "\\" + name).isDirectory() && name.startsWith("modRepart")) {
                System.out.println(name);
            }
        }

Upvotes: 1

Stas Shafeev
Stas Shafeev

Reputation: 696

I would suggest something like that:

private static void findFolders(File[] files, String fileName, List<File> foundFiles) {
    for (File child : files) {
        if (child.isDirectory()) {
            if (child.getName().startsWith(fileName)) {
                foundFiles.add(child);
            }
            findFolders(child.listFiles(), fileName, foundFiles);
        }
    }

}

Upvotes: 1

Related Questions