Solved

How to get all sub-directory and all files name in a given directory?

Posted on 2007-03-27
5
1,891 Views
Last Modified: 2013-12-14
Given a specific directory, How to get all sub-directory and all files name in this  directory?
I am using java, thanks

eric
0
Comment
Question by:ericylr
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Manish
Comment Utility
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
0
 
LVL 10

Accepted Solution

by:
Jaax earned 500 total points
Comment Utility
import java.io.File;
import java.util.ArrayList;

public class Main {
    private ArrayList searchFolders = new ArrayList();
    private ArrayList searchFiles = new ArrayList();

    public static void main(String[] args) {
        Main main = new Main();
        main.retrieveSearchFolders(new File("D:\\Temp")); //Specify ur directory
    }

    private void retrieveSearchFolders(File dir) {
        // System.out.println("Searching for Directory : " + dir);
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    System.out.println("Adding Folder : " + files[i]);
                    searchFiles.add(files[i]);
                    retrieveSearchFolders(files[i]);
                } else {
                    System.out.println("Adding File : " + files[i]);
                    searchFolders.add(files[i]);
                }
            }
        }
    }
}
0
 
LVL 11

Expert Comment

by:Manish
Comment Utility
Oh..No help from my link?
0
 

Author Comment

by:ericylr
Comment Utility
Karanw, sorry. i do not know how to assign the points to different people
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now