11

I'm trying to make a Batch file that will increment a variable by 1 each time it loops, and then check if the variable is equal to 5, and if it isn't, it loops again. I know there's probably a while loop for this, but I didn't know how to do that, and I'm just enjoying learning Batch for fun right now

Here's the code, it doesn't work the way it should, it just displays a 0: and then does nothing else. So how would I go about fixing it? I have a feeling I'm setting and incrementing the variable wrong, and maybe it's confused about the 2 if statements? (Does it have an else if....?) Anyways, thanks for the help

@echo off
set /p i=0:
goto A

:A
set /p i=i+1:
if i != 5 goto C
if i == 5 goto B

:C
echo Test :D

:B
pause>nul

Note: I don't know a lot of Batch and I'm not a pro, but I like to learn and I'm just doing this for future reference, and because I enjoy it. So, this code probably isn't good, but I want to know how I can accomplish this.

Wolverine1621
  • 307
  • 2
  • 5
  • 13

6 Answers6

28

This is a way to simulate the while loop you are trying to accomplish. Only one goto is needed:

@echo off
set /a x=0
:while
if %x% lss 5 (
  echo %x%
  pause>nul
  set /a x+=1
  goto :while
)
echo Test :D
A. Rodas
  • 20,171
  • 8
  • 62
  • 72
8

You can do that with a simple FOR command :

for /l %%x in (0,1,100) do (
echo %%x
)

You can replace 100 by the number you want

Aldrein
  • 81
  • 1
  • 1
4

To set a numerical value to a variable, you may use the /a switch:

The /A switch specifies that the string to the right of the equal sign is a numerical expression that is evaluated.

(Type SET /? for all the help).

Second, check your goto flow - this never loops back to A.

Third, check the syntax of the if expression (!= doesn't exist in batch).

marapet
  • 54,856
  • 12
  • 170
  • 184
  • Ahhhh thanks, I should've done my research on the != :D So how would I do that sort of thing then, in Batch? – Wolverine1621 Mar 16 '13 at 14:15
  • Check the documentation of `if` (`if /?`) - you either use `IF NOT %i%==5` or `if %i% NEQ 5` (Not EQual) or `if %i% LSS 5` (LeSS) – marapet Mar 16 '13 at 14:29
3

This should work:

@echo off
set var1=0

:loop
    set /a var1=%var1%+1

echo %var1%

if %var1% EQU 5 (
    goto :end
) else (
    goto :loop
)

:end
    pause
Jhon
  • 582
  • 7
  • 28
0
@echo off
set a=0
:Count
set /a a=a+1
echo %a%
goto Count
stefan
  • 10,215
  • 4
  • 49
  • 90
  • 5
    Please explain the code you propose as a solution. I'll be of way more value if you explain what you're doing. – stefan Apr 22 '15 at 20:31
0

try this:

@if (@CodeSection == @Batch) @then

@echo off
:a
cls

color c
echo --------------
echo start:
echo --------------
set /p start=
cls

echo --------------
echo start: %start%
echo --------------

echo --------------
echo stop:
echo --------------
set /p stop=
cls

echo --------------
echo start: %start%
echo --------------

echo --------------
echo stop: %stop%
echo --------------
echo. 
echo. 

echo Start in:
timeout /t 2 /nobreak >nul

echo. 5
timeout /t 1 /nobreak >nul

echo. 4
timeout /t 1 /nobreak >nul

echo. 3
timeout /t 1 /nobreak >nul

echo. 2
timeout /t 1 /nobreak >nul

echo. 1
timeout /t 1 /nobreak >nul
cls

echo --------------
echo start: %start%
echo --------------

echo --------------
echo stop: %stop%
echo --------------

echo.
echo.
echo.

echo ============================================
    set SendKeys=CScript //nologo //E:JScript "%~F0"
    %SendKeys% ">----"
    %SendKeys% "{enter}"
    :while 
    echo %start%
        %SendKeys% "%start%"
    %SendKeys% "{enter}"
    set /a start=start+1
    timeout /t 1 /nobreak >nul
    if %start% leq %stop% goto :while
goto :end

:end
echo ============================================
%SendKeys% ">----"
%SendKeys% "{enter}"

:c
echo count again? Y/N
set /p return=
if %return% == Y goto :a
if %return% == N goto :b
goto :c

:b
@end

var WshShell = WScript.CreateObject("WScript.Shell");
WshShell.SendKeys(WScript.Arguments(0));
Mr. Zero
  • 9
  • 2