Is it possible to get a variable from a string in batch?

Thomas Morse

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.

Magoo
: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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Get last character of a string from a variable Windows NT batch CMD

From Dev

batch file to get partial lastmodified string to variable

From Dev

Pagination with batch queries? Is it possible to batch gets from the datastore and get a cursor?

From Dev

Get product name from WMIC for a variable in batch

From Dev

Get a substring from a Windows batch variable

From Dev

Get last token from string in batch script

From Dev

Is it possible to change a variable from String to Number in Javascript?

From Dev

String replace with variable in batch

From Dev

Windows command batch file calling variable from string in nested loop

From Dev

Delete portion of string from variable including and after pattern with batch file

From Dev

How to get a particular line from a file into a batch variable?

From Dev

I have a string with a package name. Is it possible to get a package variable?

From Dev

I have a string with a package name. Is it possible to get a package variable?

From Dev

Batch command to get the file path only, from a string

From Dev

Batch or PowerShell: How to get the last word from string?

From Dev

How I get the position of a character from a string in batch

From Dev

How to extract number from string in BATCH and get total of all numbers?

From Dev

AngularJS - Get $scope variable from string

From Dev

How to get unique string from variable in bash

From Dev

Get property from local variable by string

From Dev

How to get hours and minutes from a String variable ?

From Dev

Get value from string representing local variable

From Dev

AngularJS - Get $scope variable from string

From Dev

Get date from date picker to string in a variable

From Dev

Get variable contents from string using javascript

From Dev

Batch file: How to get variable's value using variable's name already stored in a string

From Dev

Is it possible to store a variable in an array or object and reference it to get the value of the variable and not the string name of the variable?

From Dev

Characters in a batch file string variable

From Dev

get the size of a variable batch dimension

Related Related

  1. 1

    Get last character of a string from a variable Windows NT batch CMD

  2. 2

    batch file to get partial lastmodified string to variable

  3. 3

    Pagination with batch queries? Is it possible to batch gets from the datastore and get a cursor?

  4. 4

    Get product name from WMIC for a variable in batch

  5. 5

    Get a substring from a Windows batch variable

  6. 6

    Get last token from string in batch script

  7. 7

    Is it possible to change a variable from String to Number in Javascript?

  8. 8

    String replace with variable in batch

  9. 9

    Windows command batch file calling variable from string in nested loop

  10. 10

    Delete portion of string from variable including and after pattern with batch file

  11. 11

    How to get a particular line from a file into a batch variable?

  12. 12

    I have a string with a package name. Is it possible to get a package variable?

  13. 13

    I have a string with a package name. Is it possible to get a package variable?

  14. 14

    Batch command to get the file path only, from a string

  15. 15

    Batch or PowerShell: How to get the last word from string?

  16. 16

    How I get the position of a character from a string in batch

  17. 17

    How to extract number from string in BATCH and get total of all numbers?

  18. 18

    AngularJS - Get $scope variable from string

  19. 19

    How to get unique string from variable in bash

  20. 20

    Get property from local variable by string

  21. 21

    How to get hours and minutes from a String variable ?

  22. 22

    Get value from string representing local variable

  23. 23

    AngularJS - Get $scope variable from string

  24. 24

    Get date from date picker to string in a variable

  25. 25

    Get variable contents from string using javascript

  26. 26

    Batch file: How to get variable's value using variable's name already stored in a string

  27. 27

    Is it possible to store a variable in an array or object and reference it to get the value of the variable and not the string name of the variable?

  28. 28

    Characters in a batch file string variable

  29. 29

    get the size of a variable batch dimension

HotTag

Archive