How to iterate over an array in batch mode for key = value item

I have an array defined as LIST=(abcde) . a, b, c, d, e are set as system variables, for example. a=AAA, b=BBB , etc.

In a script package, I would like to make a for loop similar to:

 for %%i in %LIST% do echo %%i=%%%i% (unfortunately, this doesn't work) 

What I want to achieve is %%i (a) = %%%i% (%a%) , which will be resolved as a system variable, so instead of displaying %a% it will be resolved as a=AAA .

Do you have any ideas how to do this in a script package?

Thanks!

+6
source share
3 answers
 for %%i in %LIST% do CALL echo %%i=%%%%i%% 

should solve your problem.

+11
source

This is the same answer by Lorenzo Donati, but a little easier ...

 @echo off setlocal enabledelayedexpansion set LIST=(abcde) set a=value of A set b=value of B set c=value of C set d=value of D set e=value of E for %%G in %LIST% do echo %%G = !%%G! 
+3
source

It was not clear what you wanted to do. Try to find out if this solves your problem:

 @echo off setlocal enabledelayedexpansion set LIST=(abcde) set a=value of A set b=value of B set c=value of C set d=value of D set e=value of E :: deletes the parentheses from LIST set _list=%LIST:~1,-1% for %%G in (%_list%) do ( set _name=%%G set _value=!%%G! echo !_name! = !_value! ) 

The script prints the name and corresponding value of all environment variables whose names are specified in the LIST variable.

+1
source

Source: https://habr.com/ru/post/952469/


All Articles