Kwehmann
Kwehmann

Reputation: 19

FOR /L syntax explained

I have a if loop that will display all values up to the LOOPMAX. My problem is that i want this same output in the form of a FOR /L structure. I am a little confused and am wondering if this can be done.

SET LOOPMAX=6

IF %count% GTR %LOOPMAX% GOTO END
    ECHO. %test%
SET /A COUNT=%count%+1
GOTO BEGINLOOP
:END

So, if the LOOPMAX is set 6 than the output looks like:

01 test
02 test
03 test
04 test
05 test
06 test

I need that exact output from a FOR /L.

Any help is greatly appreciated, thank you in advance.

Upvotes: 0

Views: 628

Answers (2)

Aacini
Aacini

Reputation: 67216

@echo off
setlocal EnableDelayedExpansion

set count=100
for /L %%i in (1,1,%loopmax%) do (
   set /A count+=1
   echo !count:~-2! test
)

EDIT: Reply to the comment

If you want that "the values were not all test and were something different like lets say, 01 one, 02 two, 03 three", then you must prepare the desired values in advance, and then use an index to select the appropriate value in the echo command. This management may be achieved using an array:

@echo off
setlocal EnableDelayedExpansion

rem Create the array of number names
set i=0
for %%a in (one two three four five six) do (
   set /A i+=1
   set number[!i!]=%%a
)

set loopmax=6

rem Show two-digits numbers and number names
set count=100
for /L %%i in (1,1,%loopmax%) do (
   set /A count+=1
   echo !count:~-2! !number[%%i]!
)

Output:

01 one
02 two
03 three
04 four
05 five
06 six

You may review a detailed explanation on array management at this post.

Upvotes: 1

Stephan
Stephan

Reputation: 56188

setlocal enabledelayedexpansion
for /l %%i in (1,1,%loopmax%) do (
set t=00%%i
echo !t:~-2! test
)

Upvotes: 0

Related Questions