Anika Tuba
Anika Tuba

Reputation: 11

Exhaustive Nested Directory Search in Java

I was working on writing a recursive program which will take a path as input. Then it will search exhaustively all files and folders under it no matter in which depth the files and folders are in.

I've already written a java program given below :

void RecursiveFileSearch(File f, String d) {
        int i;
        String s[] = f.list();
        for (i = 0; i < s.length; i++) {
            File fs = new File(d + "/" + s[i]);
            if (fs.isDirectory()) {
                System.out.println("#DIRECTORY :"+s[i]);
                d += "/" + s[i];
                RecursiveFileSearch(fs, d);
            } else {

                System.out.println("@FILE : "+s[i]);
            }

        }
}

This function is called from outside like :

String sourceDirectoryPath = "D:";
File sourceFile = new File(sourceDirectoryPath);
RecursiveFileSearch(sourceFile,sourceDirectoryPath);

But the problem is that searches only the files and folders under the source directory only. It doesn't go into further depth.

Am I missing something?

Upvotes: 1

Views: 81

Answers (4)

1000111
1000111

Reputation: 13519

This should work:

void RecursiveFileSearch(File f, String d) {
        int i;
        String s[] = f.list();
        for (i = 0; i < s.length; i++) {
            File fs = new File(d + "/" + s[i]);
            if (fs.isDirectory()) {
                System.out.println("#DIRECTORY :"+s[i]);

                RecursiveFileSearch(fs, d + "/" + s[i]);
            } else {

                System.out.println("@FILE : "+s[i]);
            }

        }
}

Upvotes: 0

fge
fge

Reputation: 121710

Why don't you just use the APIs the JDK has for you?

With Java 8, it is as simple as:

try (
    final Stream<Path> stream = Files.walk(baseDir);
) {
    stream.forEach(path -> System.out.printf("%s: %s\n",
        (Files.isDirectory(path) ? "DIRECTORY": "FILE"), path));
}

Upvotes: 2

Philip H.
Philip H.

Reputation: 104

I dont know why you make thes work arround with Strings. Use "getAbsoluteFile" to geht the Path as a String befor you print it to the console and work only on File Objects in the programming logic. That will make your code mutch cleaner.

void RecursiveFileSearch(File f) {            
        File[] list = f.listFiles();
        for (File elem : list) {

            if (elem.isDirectory()) {
                System.out.println("#DIRECTORY:"+elem.getAbsoluteFile());

                RecursiveFileSearch(x);
            } else {

                System.out.println("@FILE : "+elem.getAbsoluteFile());
            }

        }
}

Edit: Deleted the now useless declaration if i.

Upvotes: 1

M A
M A

Reputation: 72854

The problem is that the String variable d gets changed in each recursive call to the method, each time appending a directory name to the previous directory. You can solve this by defining a new String variable instead of re-using the same one:

void RecursiveFileSearch(File f, String d) {
    int i;
    String s[] = f.list();
    for (i = 0; i < s.length; i++) {
        File fs = new File(d + "/" + s[i]);
        if (fs.isDirectory()) {
            System.out.println("#DIRECTORY :"+s[i]);
            String d2 = d + "/" + s[i];
            RecursiveFileSearch(fs, d2);
        } else {

            System.out.println("@FILE : "+s[i]);
        }

    }
}

Upvotes: 1

Related Questions