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.
Asked
Active
Viewed 91 times
2 Answers
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
-
Thanks for the answer. As I'm using java 7, isn't the answer of Aksel Willgert better? I think java.nio.file is more powerfull. – jayjaypg22 Oct 09 '17 at 10:26
-
This answer also uses `java.nio.file` – achAmháin Oct 09 '17 at 10:28