2

I am trying to check in a .bat if the VAR1 to VAR10 are not equal to "". And if so, i want to echo the content of the var. Like in the not working example below

FOR /L %%G IN (1,1,10) DO (
        IF "%SYSTEM%%%G" NEQ "" (
                echo %SYSTEM%%%G%
                ) 
        )

I am trying for hours, maybe someone have a tip for me.

jow
  • 23
  • 3
  • See: http://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990 – Aacini Apr 13 '16 at 13:58

2 Answers2

2

better use if defined and delayed expansion to get the value:

@echo off

set system1=1
set system5=9
set system10=5

setlocal enableDelayedExpansion
for /l %%# in (1,1,10) do (
    if defined system%%# (
        echo system%%# dedfined : !system%%#!
    )
)
npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

You should use EnableDelayedExpansion and verify if variables are Defined

@echo off
set Var1=
set Var2=
set Var3=3
set Var4=4
set Var5=5
set Var6=
set Var7=
set Var8=8
set Var9=9
set Var10=10
setlocal enableDelayedExpansion
FOR /L %%G IN (1,1,10) DO (
        if defined Var%%G (
            echo  Var%%G=!Var%%G!
            ) 
        )
endlocal
pause
Hackoo
  • 18,337
  • 3
  • 40
  • 70