# Set the value of the variable dynamically during runtime using bat script

**URL:** <https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360>\
**Category:** Using Jenkins\
**Created:** [December 19, 2023, 12:08pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360 "2023-12-19T12:08:27Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [December 19, 2023, 12:08pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/1 "2023-12-19T12:08:27Z")

</div>

Hello Team,  
Below is my pipeline script in JenkinsFile. I have a variable “hasError” which is initially set to false.  
In the below step, I am explicitly setting it to true . Hoowever, during echo, it prints the initialized value and not the set one.

Kindly assist

pipeline  
{  
agent any  
environment  
{  
hasError = false  
}

```
stages
{
	stage('Process Completed.') 
	{
		steps 
		{ 
			script 
			{
				bat """
					echo 'Before : %hasError%'
					set %hasError% = true
					echo 'After : %hasError%'
					"""
				echo 'Bye Bye !!!!!'
			}
		}
	}	
}

```

}

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [December 19, 2023, 12:51pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/2 "2023-12-19T12:51:44Z")

</div>

you should learn how to set environment variables in batch script. This is not Jenkins specific here.  
`set hasError=true`

---

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [December 19, 2023, 1:03pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/3 "2023-12-19T13:03:32Z")

</div>

Dear Markus,  
I did tried that, but in vain. It unfortunately did not work.

I have given u the code, can u please replicate it and show it to me?  
(Please note, that i had also tested by removing the in-between spaces)

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [December 19, 2023, 1:23pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/4 "2023-12-19T13:23:03Z")

</div>

this works for me:

```auto
node("windows") {
    bat """
					echo 'Before : %hasError%'
					set hasError=true
					echo 'After : %hasError%'
    """
}

```

and produces the following output:

```auto
14:19:54 [Pipeline] bat
14:19:55  
14:19:55 d:\workspace\pipeline_test>echo 'Before : ' 
14:19:55 'Before : '
14:19:55  
14:19:55 d:\workspace\pipeline_test>set hasError=true 
14:19:55  
14:19:55 d:\workspace\pipeline_test>echo 'After : true' 
14:19:55 'After : true'

```

---

<div class="post-metadata">

**Author:** ![p4pratik](https://avatars.discourse-cdn.com/v4/letter/p/278dde/32.png) [@p4pratik](https://community.jenkins.io/u/p4pratik)\
**Post date:** [December 19, 2023, 1:34pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/5 "2023-12-19T13:34:49Z")

</div>

Hello Marcus,  
Thanks for the same. So the change you made is using a “node” rather than a “stage”

---

<div class="post-metadata">

**Author:** ![mawinter69](https://dub1.discourse-cdn.com/flex013/user_avatar/community.jenkins.io/mawinter69/32/1625_2.png) [@mawinter69](https://community.jenkins.io/u/mawinter69)\
**Post date:** [December 19, 2023, 1:52pm UTC](https://community.jenkins.io/t/set-the-value-of-the-variable-dynamically-during-runtime-using-bat-script/11360/6 "2023-12-19T13:52:43Z")

</div>

No, I basically changed the bat step and used a proper assignement to set the env variable `hasError`  
I used a scripted pipeline and not a declarative, that is the reason it looks a bit different. In your declarative you used `agent any` which is fine as long as you only have windows (controller and agents).
