soundararajan.c soundararajan.c - 8 months ago 35
Groovy Question

How to display direcotry names in Jenkins drop down

I wish to display the directory names(not abs. path) of the Jenkins server's /tmp directory in the Jenkins Drop Down using

Active choice Reactive parameter Plugin
.

I have displayed it by using the below code.
but it was displaying along with its path in the output.

Eg output:


/tmp/directory1

/tmp/directory2


I need the output as :( without the path of the directory)


directory1

directory2


code used in groovy script part of " Active choice Reactive parameter Plugin "

import groovy.io.FileType

def list = []

def dir = new File("/tmp/")
dir.eachFileRecurse (FileType.FILES) { file ->
list << file
}

list.each {
println it.path
}

Rao Rao
Answer Source

Here you go:

Use DIRECTORIES instead of FILES

import static groovy.io.FileType.DIRECTORIES

def list = []

new File('/tmp').eachFileRecurse (DIRECTORIES) { file ->
  list << file.name
}
println list

If you do not need recursive, just use eachFile instead of eachFileRecurse.

EDIT: Based on OP's comments, Use return list instead of print list as last statement in the above script as to show list in the jenkins.