Getting paths to the files in a project

Answered

What I want to do is to construct an array of strings that will contain a path to the file, for each file in a given project. I have the instance of Project class.

One way that I have found how to do that is to use `Project.getBaseDir` to get the root directory of the project and then use `VfsUtilCore.visitChildrenRecursively` to traverse through all files and retrieve the path for them using `VirtualFile.getPath`.

However, `Project.getBaseDir` is deprecated.

The docs read:

```

use other methods depending on what you actually need:
if you need to find a root directory for a file use getContentRootForFile;
if you have a Module instance in the context, use one of its content roots;
if you just need to get a directory somewhere near project files, use guessProjectDir;
if you really need to locate .idea directory or .ipr file, use IProjectStore.

```

What I actually need is to get the VirutalFile that represents the root directory of the repository so that I can traverse that with visitChildrenRecursively. None of the described replacements for getBaseDir seem to fit my use case.

What is the best way to get the paths to all files in the project?

0
4 comments

Hi Damian,

A single project can consist of multiple modules from different paths on the file system. It can also contain multiple repositories, so there is no single root directory for all the content in the project.

For example, you can have a project in "~/Projects/my-project" and attach a module from "~/DifferentProjectsDir/my-different-project" to this project. That's why Project.getBaseDir() is deprecated - it was often misunderstood as a root for all project files/modules.

It sounds like you need to use ModuleManager.getInstance(project).getModules() to get all modules in the project, and then ModuleRootManager.getInstance(module).getSourceRoots().

EDIT:

There is also a simpler way:
https://plugins.jetbrains.com/docs/intellij/project.html#getting-a-list-of-source-roots-for-all-modules-in-a-project

0

It doesn't appear to do what I want (I tried it).

It works only, if there any directories in the project that are marked as "Sources root" and only for the files that are in that directory. In order to mark a directory as "sources root", the user has to right click the directory and click "mark as" -> "Sources root" in the IDE. Otherwise getContentSourcesRoot() and getSourcesRoot() return an empty array because there are no sources roots.

I would like to be able to get paths to all files for example in a project like on the below screenshot where there are no sources roots. In case of the project in the below screenshot ProjectRootManager.getContentSourcesRoot() and ModuleRootManaget.getSourcesRoot will return an empty array.

0

One way of doing this that I can see is to get the base path of the project using project.getBasePath and then get all files in that folder using Java standard library.

The only problem I can see with it is that .idea folder might not be stored near the project files, so getBasePath can return something unexpected.

Are there any other disadvantages of this approach? Is there any other better way of getting the paths to the files displayed on the left in the "Project" tool window?

0

Please try with:

ProjectRootManager.getInstance(project).getFileIndex().iterateContent(file -> {
System.out.println("File: " + file.getPath());
return true;
});
1

Please sign in to leave a comment.