I want to create an automated testing array in Windows Command using a single code statement as the VALUE for each testing array record. Here is what the array definition looks like for the first two records in the $code_test[xx] array:
set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (echo BUG OFF)
I want to execute the VALUE of each $code_test[xx] array record (which is a test code statement) using a for /l
loop like this:
for /l %%g in (0,1,1) do (
echo $code_test[0%%g]
!$code_test[0%%g]!
echo.
)
The complete code is:
@echo off
setlocal enabledelayedexpansion
set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (echo BUG OFF)
for /l %%g in (0,1,1) do (
echo $code_test[0%%g]
!$code_test[0%%g]!
echo.
)
echo.
pause
When I execute the complete code I get the following error message when the !$code_test[0%%g]!
line of code is executed:
'if' is not recognized as an internal or external command, operable program or batch file
I've read the excellent article here about how the IF command is parsed, but I don't see anything that jumps out at me as to why my code is failing. Is it even possible to do what I'm trying to accomplish?
Any Help Is Appreciated!
UPDATE:
I discovered that @sst is correct when I used the set
command in the set $code_test[01]=if 5 lss 10 (set /a $var1+=1)
line of code:
@echo off
setlocal enabledelayedexpansion
set $var1=0
set $code_test[00]=if 5 lss 10 (echo IT WORKED)
set $code_test[01]=if 5 lss 10 (set /a $var1+=1)
for /l %%g in (0,1,1) do (
echo $code_test[0%%g]
cmd /c !$code_test[0%%g]!
echo $var1 = !$var1!
echo.
)
echo.
pause
The set /a $var1+=1
command didn't increment the value of $var1 as expected. In fact, it places a "1" in front of the $var1 label when the line of code echo $var1 = !$var1!
executes. I haven't tried the solution offered by @Magoo yet. I'd really like to use the cmd /c
solution instead of calling a subroutine. Is there anything I can do to make the set
command work with the cmd /c
option?