sver
sver

Reputation: 942

Get the names of all the files/folders in an array

Basically I have a folder in that I have 4 zip files. I want to get the names of these zip files in an array.

Requirement : I have a folder AggregatedComponetLibraries: I have my lib zips inside it. a.zip,b.zip,c.zip,d.zip. I want to get the name of the zips insde an array componentNames in gradle that means my array should contain : a,b,c,d

Upvotes: 0

Views: 2889

Answers (1)

Stanislav
Stanislav

Reputation: 28106

You can get it with FileTree so:

def names = []
fileTree(dir: 'AggregatedComponetLibraries', include: '**/*.zip').visit {
    FileVisitDetails details ->
        names << details.file.name
}
task printNames << {
    println names
}

Here is the names array defined and then created a FileTree instance, for directory named "AggregatedComponetLibraries", this tree includes all the files with zip extention. After that, script traverses over the tree elements and add the element's names into the array.

Task printNames here is just to show the result.

Upvotes: 1

Related Questions