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

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 !!!!!'
			}
		}
	}	
}

}

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

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)

this works for me:

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

and produces the following output:

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'

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

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).