Skip to content

Instantly share code, notes, and snippets.

@yangkun
Created April 4, 2013 17:03
Show Gist options
  • Save yangkun/5312119 to your computer and use it in GitHub Desktop.
Save yangkun/5312119 to your computer and use it in GitHub Desktop.
[groovy] list dirs/files (dir first and sort as name)
import groovy.io.*
def listfiles(dir) {
dlist = []
flist = []
new File(dir).eachDir {dlist << it.name }
dlist.sort()
new File(dir).eachFile(FileType.FILES, {flist << it.name })
flist.sort()
return (dlist << flist).flatten()
}
fs = listfiles(".")
fs.each {
println it
}
@TendsToInfinity
Copy link

Well, I figured it out, and I was running it on a Linux machine.
Thanks, @herculosh for a Quick reply :)

Below is my code, Hopefully, it will useful for someone, I am running this code as a groovy script in Jenkins Pipeline

**@NonCPS
def fetFilenamesFromDir(def dir, def list){
dir.eachFileRecurse (FileType.FILES) { file ->
file = file.toString()
if (file.endsWith("json")){
list << file
}
}
}

node('master'){
def list = []

def dir = new File("dirPathToBeDefined")
fetFilenamesFromDir(dir,list)

for (i in list){
print i
print("\n")
}**

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment