1

Okay i read a lot of stuff and i failed to figure out that how to filter folders(directories and sub-dir) based on file's extension for example i want the list of folders(Directories and Sub-dir) that contains only ".mp3" or ".mp4" files and i don't want to use Walk.Tree, if there's any possible way through "filenameFilter" or any other way except Walk.tree method.

this is the code that i have tried so far, no error but also not filtering specific file's folders. it shows all the folders.

Thanks in Advance.

 File[] ff = f.listFiles(new FilenameFilter() { @Override public boolean accept(File dir, String name) { return name.toLowerCase().endsWith(".mp3") || new File(dir.toPath().toString(),name).isDirectory(); } }); 
9
  • Ok. If this is the directory structure: dir1-dir11-a.mp3, dir1-dir12-b.txt , what is that you want to output? Commented Jul 12, 2020 at 18:13
  • i want the directories that contain only mp3 files for example if the dir11,dir12 contain mp3 file and others don't then i want only dir11,dir12. Commented Jul 12, 2020 at 18:14
  • Yes but in my example. What is the output you want? Only dir11 or both dir1 and dir11 Commented Jul 12, 2020 at 18:16
  • Yes both dir1 and dir11 if the dir1 is parent and sub dir2345678910, dir11 Commented Jul 12, 2020 at 18:20
  • Do you need these names in the hierarchical order or just a set of those folder names? Commented Jul 12, 2020 at 18:29

1 Answer 1

1

Here is a simple console application.

The algorithm goes like this:

  1. First recursively search from specified root folder for all files with mp3 extension and keep adding them to a list.
  2. If this list happens to be empty, return.
  3. Else, for each of these mp3 files, recursively find their parent folders until you reach root folder and keep storing these parent folders in a set to avoid duplicate search.
  4. Now print out all those valid folders starting from root.
public class MyApp { public static void main(String[] args) { File rootDir = new File("src/main/resources/dir1"); List<File> filesListWithValidExtension = new ArrayList<>(); recursiveSearch(rootDir, filesListWithValidExtension); if (filesListWithValidExtension.size() == 0) { System.out.println("Specified root directory has no file with mp3 extension"); return; } Set<File> requiredFolders = new HashSet<>(); for (File fileWithValidExtension : filesListWithValidExtension) { File parent; do { parent = fileWithValidExtension.getParentFile(); if (requiredFolders.contains(parent)) { break; } else { requiredFolders.add(parent); fileWithValidExtension = parent; } } while (!parent.getPath().equals(rootDir.getPath())); } System.out.println("Printing folders (from root) containing mp3 files"); recursivePrint(rootDir, requiredFolders); } private static void recursiveSearch(File file, List<File> filesListWithvalidExtension) { if (!file.exists()) { return; } if (file.isDirectory()) { for (File f : file.listFiles()) { recursiveSearch(f, filesListWithvalidExtension); } } if (file.getName().toLowerCase().endsWith(".mp3")) { filesListWithvalidExtension.add(file); } } private static void recursivePrint(File file, Set<File> requiredFolders) { if (!requiredFolders.contains(file)) { return; } System.out.println(file.getPath()); for (File f : file.listFiles()) { recursivePrint(f, requiredFolders); } } } 

This program outputs like:

Printing folders (from root) containing mp3 files src/main/resources/dir1 src/main/resources/dir1/dir12 src/main/resources/dir1/dir11 src/main/resources/dir1/dir11/dir111 
Sign up to request clarification or add additional context in comments.

4 Comments

i tried but stack overflow said "Thanks for the feedback! Votes cast by those with less than 15 reputation are recorded, but do not change the publicly displayed post score."
ohh it's ok. Thanks a lot
by the way i am new here and again Thank you for your valuable time.
Happy to help :-)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.