java - Get all folders with a given name -


i searching solution find folders same name in given directory.

so folder structure looks this:

                                       root                          |                |             |                      android          windows          ios                     |       |        |       |       |      |                    focus    normal  focus   normal  focus   normal 

note: there more folders between clients , iconsets, that's why need recursion.

i want arraylist pathes of e.g. normal folders. although recursion confuses me lot time couldnt it.

this first try, should return all contained directories in root folder (parameter path). string iconset should define name of searched folder afterwards.

private static arraylist<string> getalliconsetfolders(string path, string iconset) {         arraylist<string> pathes = new arraylist<string>();          file folder = new file(path);         file[] listoffiles = folder.listfiles();          (file file : listoffiles) {             if (file != null && file.isdirectory()) {                 pathes.addall(getalliconsetfolders(file.getabsolutepath(), iconset));             }         }         return pathes;     } 

it return empty arraylist in case.

how can paths (the normal folders when string iconset = "normal") result like:

  • "root/android/[...]/normal"
  • "root/windows/[...]/normal"
  • "root/ios/[...]/normal"

i've tested following code , appears work correctly:

public static list<file> finddirectorieswithsamename(string name, file root) {   list<file> result = new arraylist<>();    (file file : root.listfiles()) {     if (file.isdirectory()) {       if (file.getname().equals(name)) {         result.add(file);       }        result.addall(finddirectorieswithsamename(name, file));     }   }    return result; } 

your original code there, omitted part add matching directories result list.


tested with:

c:\tmp\foo c:\tmp\foo\bar c:\tmp\foo\baz c:\tmp\foo\baz\foo c:\tmp\foo\baz\foo\bar 

using

public static void main(string[] args) throws exception {   list<file> files = finddirectorieswithsamename("foo", new file("c:\\tmp"));    (file f :files) {     system.out.println(f);   }     } 

output:

c:\tmp\foo c:\tmp\foo\baz\foo 

Popular posts from this blog

How to calculate SNR of signals in MATLAB? -

c# - Attempting to upload to FTP: System.Net.WebException: System error -

ios - UISlider customization: how to properly add shadow to custom knob image -