I am having problems getting the desired result in cmd batch file. I need to print the result in the below mentioned format
@echo off
setlocal enabledelayedexpansion
for %%i in (8 7 6 5 4 3 2) do (
echo %%i
if %%i EQU 8 goto lvl8
if %%i EQU 3 goto lvl35
if %%i EQU 2 (goto lvl3) else goto lvls
:lvl8
set str1=8
set str2=16
:lvl35
set str1=3.5
set str2=4
:lvl3
set str1=3
set str2=3.5
:lvls
set str1=%%i
set /A str2=%%i+1
echo %%i
rem echo.!str!
echo.!str1!
echo.!str2!
)
endlocal
Expected outputs
8
8
16
7
7
8
6
6
7
5
5
6
4
4
5
3
3.5
4
2
3
3.5
Right now am getting the following:
8
Missing operand
%i
%i
3.5
EDIT: updated the above question as per Joey’s solution.
%i%has no value. Were you intending to use%%i?To clarify, what
cmdsees here:which clearly is a syntax error.
Ok, after your edit, there are a few problems, still. If you use
gotoyou apparently leave theforloop. Thus, after agoto%%idoesn’t have a value, either and the loop only runs once.Then there is the way you use the
gotos; agotois a jump to a specific line in code. Program flow continues normally after that and thus if you jump tolvl8you end up running all subsequent lines, too, even those after:lvl35and:lvl3or:lvls. After each section you jump to you’d have to include an explicit jump to the end of the loop to skip the unwanted lines. But that’s semi-beside the point, as you cannot usegotowithin theforloop, as mentioned above.So the first thing you should do is get rid of
gotoand instead use structured programming.ifcan accept a block, too. Then you output%%ionce too often, which can easily be corrected, too.It now looks like this here:
Note that each
ifalso has anelseto avoid running the lastelsefor all numbers except2. The output is now correct. Code can be found in my SVN repository, too.