0

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.

jayjaypg22
  • 1,641
  • 5
  • 22
  • 41

2 Answers2

1

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);
        }
    }

}
Stas Shafeev
  • 606
  • 6
  • 8
1

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);
            }
        }
achAmháin
  • 4,176
  • 4
  • 17
  • 40