2

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?

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
Ben Crazed Up Euden
  • 353
  • 2
  • 7
  • 18
  • What do you meant by *partial file paths*? Here in your post it is root relative `/home/user/test` – KV Prajapati Sep 20 '12 at 03:01
  • If you have absolute paths, simply chop off their initial part equal to your directory, e.g. `/home/user/test/quick/brown/fox.txt` becomes `quick/brown/fox.txt` after chopping off the initial `/home/user/test/` part. – Sergey Kalinichenko Sep 20 '12 at 03:08

4 Answers4

3

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

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

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, "");
Hilikus
  • 9,954
  • 14
  • 65
  • 118
0

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);
    }
}
Osiris
  • 4,195
  • 2
  • 22
  • 52
  • Okay, is there an easy way to separate them without having to specify the count of the string? because the substring function takes an int but getting the length of a string returns a long and casting it causes issues. – Ben Crazed Up Euden Sep 20 '12 at 03:11
  • What do you mean by 'count' of a string? The length of a string is almost never going to be outside the range of int. You can safely use (int)longLength. – Osiris Sep 20 '12 at 03:50
0

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

Vikdor
  • 23,934
  • 10
  • 61
  • 84