Is it possible to change environment variables within Batch and utilize them without restarting CMD prompt?

I want to setup a system wide environment variable within my batch script script running within the CMD prompt, I have been able to achieve this by calling:

setx MyEnvVar "C:\<Some Path>" /M

However when I do:

echo "MyEnvVar is %MyEnvVar%"

afterwards the statement that outputs at prompt is "MyEnvVar is" although the variable has been setup with setx and I can observe it through looking at the Windows - system properties - environment variables GUI.

I know this is because the CMD prompt has to be restarted for it to pick up the new environment variables however I don't want to stop the execution of my batch script and tell the user to manually restart the CMD prompt window and re-run my script so the environment variables are picked up. Is there some other way of getting around this?

It would be better if I could get around this without utilizing the "call" method and breaking the script to two segments

batchfileA - Code up until and including the call to change the environment variables thereafter utilize call method to call batchfileB batchfileB- The rest of the original code placed inside batchfileB and called with call method within batchfileA

--------------Solutions-------------

I had tried using set after the setx and echoed the result and the variable was null so I assumed that the value was not taking because I had to restart the CMD prompt what I forgot was I had enabled DelayedExpansion and had to use ! (exclamation marks) instead of % (percent) signs around my variable names

Category:windows Time:2017-10-19 Views:84

Related post

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 0.822 (s). 14 q(s)