Windows command line: setting global variables to each other
scenario further updated
Hi all:
I'm trying to set a global variable to another in windows command line batch language. The syntax I've tried is as below:
:: these are global vars
SET varThree=%varOne%\something
SET varOne=
SET varTwo=2
:Section
SET varOne=%varTwo%
::more setting of varOne to other global variables
GOTO Section2
:Section2
echo %varThree%
GOTO cleanup
:cleanup
SET va开发者_开发问答rThree=
SET varTwo=
SET varOne=
The value of varOne seems to be lost when it comes to echoing %varThree%.
I was wondering how can I set the above variable properly?
TIA.
EDIT: The cleanup section was there as a way to ensure the variables are being cleaned up upon exiting of the program. When I removed it, it took the 2nd run in order for varOne to lose its state. Likewise when I re-introduced it, it took the 2nd run for the state of varOne to come back. Any ideas why is this happening?
You don't use the percent signs on the left side. Also, you might want to read about delay-expanded variables (like !varTwo! instead of %varTwo%), since you might run into problems with incorrect expansions. See this link about delayed expansions: Cmd.exe Documentation
Edit: You're also missing a "Set" on the left, so this:
:Section
%varOne%=%varTwo%
Should really be:
:Section
Set varOne=%varTwo%
Try:
varOne=%varTwo%
You are wrong in looking for a "post-compiling" syntax. In your scenario
SET varThree=%varOne%\something
cause varThree set with "\something", because varOne is not defined yet. Speaking about your scenario, you have to use
echo %varOne%%varThree%
in ":section2".
精彩评论