I don't know where this question came from, but... Is there a way to start at one directory, say... C:\ . and I wanted to go into that folder, and printout a list of the folders/files in that directory, and then do the same for the rest of the folders that exist for so many levels down?

I want it to look something like this:

C:\
--> Documents and Settings
----> All Users
------> // files/folders in All Users
----> User1
------> // files/folders in User1
----> // files in Documents and Settings
--> // other folders/files

I'm just curious if something like that is possible, and if so, how I would start something like that. I know it would end up being recursive, and that it would be useful to ask for a starting location and depth to check, but other than that, I have no idea where to start.

Recommended Answers

All 4 Replies

Here's a skeletal recursion for it. You just need to figure out the depth part

public void listFiles(File dir){
    for (File file : dir.listFiles()){
        if (file.isDirectory()){
            listFiles(file);
        } else {
            // whatever
        }
    }
}

I got the code to work, but how does that for loop function? I've seen the same thing done with String objects, but I've never used/understood them... i've only used the: for(int z=0;z<#;z++) structure....

thanks, now it make sense =]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.