#Files.walk ignore source directory

3 messages · Page 1 of 1 (latest)

normal whale
#

I am using Files.walk to display a file tree but it is showing the source directory in the file tree. How can I get rid of it?
main method

    public static List<String> getFileTree(String sourceDirectoryLocation) {
        List<String> tree = new ArrayList<>();
        Path path = Paths.get(sourceDirectoryLocation);
        int sourceCount = path.getNameCount();
        try (Stream<Path> pathStream = Files.walk(path, FileVisitOption.FOLLOW_LINKS)) {
            pathStream.forEach(file -> {
                String indentation = " ".repeat((file.getNameCount() - sourceCount) * 4);
                String fileColor;
                if (file.getFileName().toString().endsWith(".sk")) {
                    fileColor = file.getFileName().toString().startsWith("-") ? "<red>" : "<green>";
                } else if (Files.isDirectory(file)) {
                    fileColor = "<gold>";
                } else {
                    fileColor = "<white>";
                }
                tree.add(indentation + (indentation.equals("") ? "" : "<gray>└ ") + fileColor + file.getFileName());
            });
        } catch (IOException e) {
            throw new RuntimeException("Error while getting a file tree.", e);
        }
        return tree;
    }

how i'm using it

case "list" -> FileUtils.getFileTree("./plugins/Skript/scripts/")
        .forEach(message -> send(sender, message));

as you can see in the image, it displays "scripts" which is the source directory

pliant widgetBOT
#

This post has been reserved for your question.

Hey @normal whale! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.