Jenkins groovy batch error when trying to use find command to set value of a variable in a text file to some other variable

Viewed 89

I have a file called test.txt in jenkins workspace directory for the pipeline job being run on windows slave agent. The contents of the file is as :-

test1=abc  
test2=xyz

Now i am running a pipeline groovy job which is :-

node () {

 bat '''FOR /F "tokens=2 delims==" %%a IN ('find "test2" ^<%WORKSPACE%\\test.txt') DO SET test2value=%%a
        echo %test2value%'''
}

If i execute the same script using windows command line on the same windows slave machine it works just fine but in jenkins groovy it says file test2 not found. I am unable to figure out how to use this in this scenario. The actual expected result of this pipeline should be xyz Kindly help

1 Answers

This works for me. Are you sure the text file is there?

node("windows"){
    stage("dummy"){
        bat """
@where find
@echo test1=abc>test.txt
@echo test2=xyz>>test.txt
@type test.txt

@FOR /F "tokens=2 delims==" %%a IN ('find "test2" ^<%WORKSPACE%\\test.txt') DO @SET test2value=%%a
@echo %test2value%
        """
    }
}

output:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (dummy)
[Pipeline] bat
C:\Windows\System32\find.exe
test1=abc
test2=xyz
xyz
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node

Make sure that you actually using the Windows find.exe and not the find from bash that might come from your git installation.

Related