canicani
canicani

Reputation: 13

Eclipse plugin development - How to get the list of sub folders and keep navigating the folder?

I'm developing a file search module in Eclipse plugin development. Now, I know the name of the path using IJavaProject.getOutputLocation(), and I want to get all folders and files under the IPath directory.

I can see the APIs that create or get IFolder from the folder name but I cannot find the thing that can return the list of the folders under a specific directory.

Could you please let me know how to et the list of sub folders from IPath?

Thanks a lot.

Upvotes: 1

Views: 1810

Answers (1)

isnot2bad
isnot2bad

Reputation: 24444

An IFolder is an IContainer, so you can get a list of all sub-elements by calling members(). It will return an IResource[]. Test the actual type of each element via instanceof - it should be either IFolder or IFile.

An IPath is just a list of string segments, separated by '/'. Usually it is used to point to a file within the workspace, but this is not mandatory.

The method IJavaProject#getOutputLocation() returns a (workspace-relative) absolute path to the output folder of your java project (that's usually the bin directory where all the compiled stuff and other generated resources are stored).

IPath outputPath = project.getOutputLocation();

If you have a path that is workspace relative, you can get the actual IFile or IFolder by asking an IContainer - for example the IWorkspaceRoot (which is the root container of the workspace):

IFolder outputFolder = ResourcesPlugin.getWorkspace().getRoot().getFolder(outputPath);

Now you could traverse the folder structure:

void printStructure(IContainer container, String intent) {
    for(IResource member : folder.members()) {
        System.out.println(intent + member.getName());
        if (member instanceof IContainer) {
            printStructure((IContainer)member, intent + "  ");
        }
    }
}

Upvotes: 2

Related Questions