Ben Crazed Up Euden
Ben Crazed Up Euden

Reputation: 353

List files from directories and sub directories in java including only partial file paths

I need to get the paths of files and their parent directories in java from a given directory but not including it.

So for example, If my method was given the path: /home/user/test as a path it would return the paths of all files in that directory and under it.

So if /home/user/test had the sub folders: /subdir1 and /subdir2 each containing file1.txt and file2.txt then the result of the method would be 2 strings containing /subdir1/file1.txt and /subdir2/file2.txt

And if subdir1 had a directory inside it called subsubdir and inside that file3.txt, then the string created for that file would be /subdir1/subsubdir/file3.txt, and if there are further sub directories that would continue.

The idea is I just want the directory paths above the file but not the absolute path so only the directories AFTER the initial given path.

I know its a little confusing but I'm sure someone can make sense of it. Right now all I have is a recursive function that prints out file names and their absolute paths.

Any assistance on this?

Upvotes: 2

Views: 4376

Answers (4)

MadProgrammer
MadProgrammer

Reputation: 347184

What would have been nice if you had tried something and asked questions about that...

However...

public class TestFileSearch {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new TestFileSearch();
    }

    public TestFileSearch() {
        File parentPath = new File("C:/Users/shane/Documents");
        List<String> files = list(parentPath);

        for (String file : files) {
            System.out.println(file);
        }
    }

    protected List<String> list(File parent) {
        return listFiles(parent, parent);
    }

    protected List<String> listFiles(File parent, File folder) {
        List<String> lstFiles = new ArrayList<String>(25);
        if (folder.isDirectory()) {

            File[] files = folder.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        lstFiles.addAll(listFiles(parent, file));
                    } else {
                        String path = file.getPath();
                        String offset = parent.getPath();

                        path = path.substring(offset.length());
                        lstFiles.add(path);
                    }
                }
            }
        }

        return lstFiles;
    }
}

You could simply do a normal folder recursion, returning a list of files and THEN strip of the prefix, but that's up to you

Upvotes: 3

Hilikus
Hilikus

Reputation: 10331

What about using the absolute path you currently have but removing the prefix from it using String.replace

You said you had the full, absolute path, say in full
then just do

String relative = full.replace(prefix, "");

Upvotes: 1

Vikdor
Vikdor

Reputation: 24124

For each file found, print the file.getAbsolutePath().substring(rootPath.length());

Upvotes: 0

Osiris
Osiris

Reputation: 4185

If you have the input "/home/user/text", all absolute paths to files will start with /home/user/text/. If you're already able to print a list of all files under text/, then all you need to do is take the suitable substring.

The following function should visit all files under pathToDir. In the printFileName function, you can remove the /home/user/text part and print the file names

public static void gotoAllFiles(File pathToDir) {
    if (pathToDir.isDirectory()) {
        String[] subdirs = pathToDir.list();
        for (int i=0; i<subdirs.length; i++) {
            gotoAllFiles(new File(pathToDir, subdirs[i]));
        }
    } else {
        printFileName(pathToDir);
    }
}

Upvotes: 0

Related Questions