I've recently started to learn batch for the sake of writing batch sims for a game that I've been playing. I was wondering if its possible to somehow iterate through like named variables (since I can't seem to find anything about a list?). Also I'm not sure if I can put a label to call to as a variable passed.
Code Example:
:: Enemy Fortress level.
SET EFORTLVL=4
:: Don't mess with anything below here only the variables above.
:: Enemy Fortress that will be simmmed against. Note this batch sim is only built to run against one tower, as this is what you should be doing.
SET EFORTRESS1="Foreboding Archway-%EFORTLVL%"
SET EFORTRESS2="Illuminary Blockade-%EFORTLVL%"
SET EFORTRESS3="Tesla Coil-%EFORTLVL%"
SET EFORTRESS4="Minefield-%EFORTLVL%"
SET EFORTRESS5="Forcefield-%EFORTLVL%"
call :sim 1
:sim
SET /a "COUNTER=1"
SETLOCAL enabledelayedexpansion
SET times=!ITERATIONS%1!
ENDLOCAL & SET TIMES=%times%
:whilesim
SETLOCAL enabledelayedexpansion
SET fort=!EFORTRESS%COUNTER%!
ENDLOCAL & SET FORT=%fort%
tuo.exe %DECK0% %ENEMY% surge random -v efort %FORT% yfort %YFORTRESSES% climb %TIMES% >> %PATH%\WarDefClimbData%DECK0%.txt
SET /a "COUNTER=COUNTER+1"
if %COUNTER% leq 5 GOTO :whilesim else GOTO :eof
The result that I get for the line on the console:
RESOLVED: What I want to do is get a value from a variable that holds a string name that relates to the variable in question. (Ex when the for loop passes 1 I want to get EFORTRESS1 value, 2 I want EFORTRESS2 value etc).
E:\Programs\Tyrant Unleashed Optimizer>tuo.exe oconzer "VetDecks" surge random -v efort EFORTRESS1 yfort "Inspiring Altar #2" climb ITERATIONS1 1>>"e:\Programs\Tyrant Unleashed Optimizer\BatchSimResults"WarDefClimbDataoconzer.txt
Error: unrecognized fortress EFORTRESS1
Now I understand why its saying the error, what I don't understand is why its not getting the value from the string that is contained in FORT.
RESOLVED Getting an endless loop, where the iteration variable isn't updating.
:sim
SETLOCAL ENABLEDELAYEDEXPANSION
SET "FORT=!EFORTRESS%1!"
ENDLOCAL&SET "fort=%fort%"
SET TIMES=ITERATIONS%2
SET LABEL=%3
The issue is to get the contents of (the contents of a variable), often call "indirection".
This is probably the easiest way. It uses setlocal enabledelayedexpansion
which places cmd
in delayedexpansion
mode, where !var!
is evaluated after its contents.
The drawback is that it must be invoked using setlocal
, which establishes a local environment. The loacl environment must eventually be closed (you cannot keep opening more) and at that time, all changes to the environment are discarded and it is restored to its state at the point of executing setlocal
.
The endlocal&...
uses a parsing trick to transfer the changes out of the setlocal/endlocal
bracket.
As for the other questions - yes, you can goto
a variable (and the contents of the variable need not have a leading colon). It is quite possible to use goto somewhere%1
for instance, and supply %1
as a parameter as you've done. The text somewhere
would be simply prepended to the value %1
.
BTW - it would appear that you are changing path
. This is not a good idea. path
is the variable that contains a ;
-separated list of directories which are searched for an executable if that executable is invoked and not found in the current directory. Best left well alone. Same goes for tmp
and temp
(which point to a temporary directory) and date
and time
and random
and cd
(the current date, time, a random number and the current directory)
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments