Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Getting folder path - recursion

  1. #1
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Getting folder path - recursion

    In a random set of nested folders and files, I need to get all folder paths where a particular child folder' name is say "x".

    For example, in following structure, I need the program to return back only a\c\e\x

    ProblemStmt.jpg

    Please see that there can be any level of nesting and any folder structure. Thanks for help.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Getting folder path - recursion

    The File class has methods that will help you with this. What have you tried so far?
    Do you have any specific questions about your program?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    May 2012
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Getting folder path - recursion

    Quote Originally Posted by Norm View Post
    The File class has methods that will help you with this. What have you tried so far?
    Do you have any specific questions about your program?
    I have been able to write a basic program to achieve what I wanted. However, it fails when in my previous diagram there is a directory 'dir x' under 'dir d'. So the program returns a\c\e\x and d\x. Can you help make that fix?

    public class Zombie {

    String folderName= new String();
    ArrayList folderList= new ArrayList();

    public static void main(String[] args) {
    String path;
    try {
    path = new java.io.File(".").getCanonicalPath();
    Zombie zombie= new Zombie();
    getFilePaths(new File(path).listFiles(), zombie);


    Iterator itr = zombie.folderList.iterator();
    while (itr.hasNext())
    System.out.println(itr.next());

    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    public static void getFilePaths(File[] fileList, Zombie zombie) {
    for (int i=0; i<fileList.length; i++) {
    File file= fileList[i];
    if (file.isDirectory()) {
    if (containsDesiredFolder(file)) {
    zombie.folderList.add(zombie.folderName+ "\\" + file.getName());
    zombie.folderName="";
    } else {
    if (containsFoldersNotFiles(file)) {
    zombie.folderName= zombie.folderName + "\\" + file.getName();
    getFilePaths(file.listFiles(), zombie);
    }
    }
    }
    }
    }

    public static boolean containsFoldersNotFiles(File file) {
    boolean flag = false;
    if (file.isFile()) return false;

    File[] dummyList = file.listFiles();

    //no file or folder present
    if (dummyList == null)
    return flag;

    for (int i = 0; i < dummyList.length; i++) {
    File dummyFile = dummyList[i];
    if (dummyFile.isDirectory() ) {
    return true;
    }
    }
    return flag;
    }

    public static boolean containsDesiredFolder(File file) {
    boolean flag = false;
    if (file.isFile()) return false;
    File[] dummyList = file.listFiles();

    if (dummyList == null)
    return flag;

    for (int i = 0; i < dummyList.length; i++) {
    File dummyFile = dummyList[i];
    if (dummyFile.isDirectory() && dummyFile.getName().equals("en_US")) {
    flag = true;
    }
    }
    return flag;
    }
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,130
    Thanks
    48
    Thanked 2,156 Times in 2,129 Posts

    Default Re: Getting folder path - recursion

    Please Edit your post and wrap your code with[code=java]<YOUR CODE HERE>[/code] to get highlighting

    Post the program's output and explain what is wrong with it and show what you want it to be.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. How to convert local path drive to UNC path
    By krisswift in forum Object Oriented Programming
    Replies: 2
    Last Post: November 17th, 2011, 07:40 AM
  2. Using recursion to calculate a path through an array
    By jpetticrew in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 20th, 2011, 05:21 PM
  3. Relative path issue with Context path struts
    By chinnu in forum Web Frameworks
    Replies: 1
    Last Post: March 31st, 2011, 10:17 AM
  4. Getting information from a folder
    By shadihrr in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: February 23rd, 2010, 03:13 PM
  5. how to get full path name from realtive path
    By priyanka3006 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: August 10th, 2009, 04:28 AM