0
  • I've got a set of variables called p1 to p9
  • I've got 2 vars to set the range I'm interested in.
  • I want to list all the vars from minimum range +1 to the max.

It looks like that (and it won't work)

set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo %p %currentvar% % //IT WON'T WORK AND I DON'T KNOW HOW TO MAKE IT WORK...
goto LOOP
) else (
goto END
)
:END

The result I'd like to see:

fff

ggg 

hhh

iii
1
  • you marked a non-working answer as accepted. Commented Nov 7, 2013 at 0:09

2 Answers 2

3

this might work for you:

@ECHO OFF &SETLOCAL
set /a currentvar=5
set /a maxvar=9
set /a RangeStart=currentvar+1
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result=maxvar-currentvar
echo Found %result% vars in the range.
for /l %%a in (%RangeStart% 1 %maxvar%) do call echo(%%p%%a%%
Sign up to request clarification or add additional context in comments.

Comments

-1

You need to Enabledelayedexpansion:

@echo off
Setlocal Enabledelayedexpansion
set currentvar=5
set maxvar=9
set p1=aaa
set p2=bbb
set p3=ccc
set p4=ddd
set p5=eee
set p6=fff
set p7=ggg
set p8=hhh
set p9=iii
set /a result = %maxvar% - %currentvar%
echo Found %result% vars in the range.
:LOOP
if %currentvar% LSS %maxvar% (
set /a currentvar=%currentvar% + 1
echo !p%currentvar%! &REM This is how you make a comment in batch
goto LOOP
) else (
goto END
)
:END
Endlocal

And that should do what you want to do. Also to make a comment use a new line and type :: or REM.

Mona

2 Comments

Thank you very much. I didn't put any comments in batch in ages. I didn't knew that you can use ! to expand vars. That is exactly what I needed. You are awesome ;].
this doesn't work. Inside a code block you can't access changed %variables%, eg. currentvar !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.