How to get files and directories name using Gradle?
Asked Answered
S

2

25

I have dir in which another task create files and directories so in this dir there are files, directories, subdirectroies, files in them and ect. I want to put all absolute path of files and directories in a list.

def listNames = project.fileTree('dir')

But only the files are included in the list, directories are missing. How to collect all of them?

Singlephase answered 11/3, 2014 at 7:31 Comment(0)
B
35
def names = []
fileTree("baseDir").visit { FileVisitDetails details -> 
    names << details.file.path 
}

For further details, see FileTree in the Gradle Javadoc.

Bechler answered 11/3, 2014 at 12:29 Comment(1)
How do I get access to the FileVisitDetails type? I tried an import but no dice.Sicken
F
28

The shorter version:

def files =  fileTree("dirName").filter { it.isFile() }.files.name

Of course it does the same thing.

Farfamed answered 26/10, 2015 at 10:3 Comment(1)
Actually, your version just collects out files, while Peter's version collects directories and files. So they aren't the same.Jacie

© 2022 - 2024 — McMap. All rights reserved.