0

I want to set level, environment and version using three environment variables to run a test multiple times with different lvl, env and ver. I already have three environment variables(array/list) from where I can read all three values to be set.

I want to iterate over three lists to get lvl, env and ver simultaneously. I want it to be LVL1, ENV1, VER1 for first run then LVL2, ENV2, VER2 for next and so on.

I have these set in command window from where I will be launching my process

set LVL_VAR=LVL1 LVL2 LVL3
set ENV_VAR=ENV1 ENV2 ENV3
set VER_VAR=VER1 VER2 VER3

I can read these three to get lvl, env, ver Then I need to set variables LVL, ENV and VER as LVL1, ENV1 and VER1 and so on before running RunTest.bat

This is what i tried, nested loop, but i dont know how to restrict 2nd and 3rd loop to set it correctly or if there is another way to do it.

@echo OFF
for %%L in (%LVL_VAR%) do call :for_lvl %%L
    
exit /b

:for_lvl
    set LVL=%1
    echo LVL %LVL%
    for %%E in (%ENV_VAR%) do call :for_env %%E
    exit /b
    goto :cont
  :cont
exit /b

:for_env
    set ENV=%1
    echo ENV %ENV%
    for %%V in (%VER_VAR%) do call :for_ver %%V
    goto :cont
  :cont
    exit /b
exit /b

:for_ver
    set VER=%1
    echo VER %VER%
    echo =======================RunTest.bat===================
exit /b

The obvious output i get is

LVL LVL1
ENV ENV1
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV2
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV3
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
LVL LVL2
ENV ENV1
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV2
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV3
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
LVL LVL3
ENV ENV1
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV2
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================
ENV ENV3
VER VER1
=======================RunTest.bat===================
VER VER2
=======================RunTest.bat===================
VER VER3
=======================RunTest.bat===================

I need it to be like this

LVL LVL1
ENV ENV1
VER VER1
=======================RunTest.bat===================
LVL LVL2
ENV ENV2
VER VER2
=======================RunTest.bat===================
LVL LVL3
ENV ENV3
VER VER3
=======================RunTest.bat===================

How do I limit my iterations or is there other way?

vishwain
  • 1
  • 1
  • Does this answer your question? [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script) – T3RR0R May 11 '23 at 08:11

2 Answers2

0

Lists and arrays are very different things. These are lists:

set LVL_VAR=LVL1 LVL2 LVL3
set ENV_VAR=ENV1 ENV2 ENV3
set VER_VAR=VER1 VER2 VER3

You can convert lists into arrays this way:

setlocal EnableDelayedExpansion

set n=0
for %%a in (%LVL_VAR%) do set /A n+=1 & set "LVL[!n!]=%%a"
set n=0
for %%a in (%ENV_VAR%) do set /A n+=1 & set "ENV[!n!]=%%a"
set n=0
for %%a in (%VER_VAR%) do set /A n+=1 & set "VER[!n!]=%%a"
rem Keep here the last value of %n%

... and then process the three arrays at same time via the subscript or index:

for /L %%i in (1,1,%n%) do (
   echo LVL !LVL[%%i]!
   echo ENV !ENV[%%i]!
   echo VER !VER[%%i]!
   echo =======================RunTest.bat===================
)

I suggest you to review this answer

Aacini
  • 65,180
  • 12
  • 72
  • 108
0

A simple solution for the task according to the description would be:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LVL_VAR=LVL1 LVL2 LVL3"
set "ENV_VAR=ENV1 ENV2 ENV3"
set "VER_VAR=VER1 VER2 VER3"
(for /L %%I in (1,1,3) do call :RunTest %%I) & exit /B
:RunTest
for /F "tokens=%1" %%J in ("%LVL_VAR%") do set "LVL=%%J"
for /F "tokens=%1" %%J in ("%ENV_VAR%") do set "ENV=%%J"
for /F "tokens=%1" %%J in ("%VER_VAR%") do set "VER=%%J"
echo LVL %LVL%
echo ENV %ENV%
echo VER %VER%
echo =======================RunTest.bat===================
goto :EOF

The four echo command lines should be replaced by call RunTest.bat. It is also possible to include the code of RunTest.bat in this batch file after the three for /F loops replacing the four echo command lines and perhaps even goto :EOF for reducing the file system accesses on running this batch file.

There can be also used a different delimiter instead of a space in case of the three values of the three environment variables contain itself spaces like a horizontal tab character or a vertical bar as shown below:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LVL_VAR=LVL1|LVL2|LVL3"
set "ENV_VAR=ENV1|ENV2|ENV3"
set "VER_VAR=VER1|VER2|VER3"
(for /L %%I in (1,1,3) do call :RunTest %%I) & exit /B
:RunTest
for /F "tokens=%1 delims=|" %%J in ("%LVL_VAR%") do set "LVL=%%J"
for /F "tokens=%1 delims=|" %%J in ("%ENV_VAR%") do set "ENV=%%J"
for /F "tokens=%1 delims=|" %%J in ("%VER_VAR%") do set "VER=%%J"
echo LVL %LVL%
echo ENV %ENV%
echo VER %VER%
echo =======================RunTest.bat===================

To understand the commands used and how they work, open a command prompt window, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.

  • call /?
  • echo /?
  • exit /?
  • for /?
  • goto /?
  • set /?
  • setlocal /?
Mofi
  • 46,139
  • 17
  • 80
  • 143