OK999
OK999

Reputation: 1399

For loop in groovy with two list of variables

i have a list of batch commands that performs xcopy 's, like this. This is for a jenkins pipeline script:

        bat "xcopy %cd%${some_path1}\\obj\\Lab5\\Package\\PackageTmp ${host1}\\dir1 /E /Y /V"
        bat "xcopy %cd%${some_path2}\\obj\\Lab5\\Package\\PackageTmp ${host1}\\dir2 /E /Y /V"
        bat "xcopy %cd%${some_path3}\\obj\\Lab5\\Package\\PackageTmp ${host1}\\dir3 /E /Y /V"

How do i use a loop in groovy, so that i can have only one line of the batch command and pass the value of the variables ("some_path1", "some_path2", "some_path3") & ("dir1", "dir2", "dir3")

Upvotes: 1

Views: 2017

Answers (1)

tim_yates
tim_yates

Reputation: 171054

So, given a list of paths:

def paths = ['some_path1', 'some_path2', 'another_path']

And a list of directories for each path (in a 1:1 relationship)

def dirs = ['dir1', 'dir_for_some_path2', 'another_dir']

You can then do:

[paths, dirs].transpose().each { path, dir ->
    bat "xcopy %cd%${path}\\obj\\Lab5\\Package\\PackageTmp host1\\${dir} /E /Y /V"
}

In case you have an ancient version of groovy, try

[paths, dirs].transpose().each { pd ->
    def path = pd[0]
    def dir = pd[1]
    bat "xcopy %cd%${path}\\obj\\Lab5\\Package\\PackageTmp host1\\${dir} /E /Y /V"
}

Upvotes: 3

Related Questions