"Counter" in the package

I am trying to make a batch file that will increment a variable by 1 with each cycle, and then check if the variable is 5, and if not, then it repeats again. I know there is probably a while loop for this, but I did not know how to do this, and I am just enjoying learning Batch for fun right now.

Here's the code, it doesn't work as it should, it just displays 0: and then does nothing. So how can I fix this? It feels like I'm setting and incrementing a variable incorrectly, and maybe it gets confused with 2 if statements? (Does he have more if ....?) In any case, thanks for the help

@echo off set /pi=0: goto A :A set /pi=i+1: if i != 5 goto C if i == 5 goto B :C echo Test :D :B pause>nul 

Note. I don’t know much Batch, and I’m not a professional, but I like to study, and I just do it for future reference and because I like it. So this code is probably not very good, but I want to know how I can do this.

+6
source share
6 answers

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

 @echo off set /ax=0 :while if %x% lss 5 ( echo %x% pause>nul set /a x+=1 goto :while ) echo Test :D 
+23
source

You can do this with a simple FOR command:

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

You can replace 100 with the desired number

+4
source

To set a numeric value for a variable, you can use the /a switch:

The / A switch indicates that the line to the right of the equal sign is a numeric expression that is evaluated.

(Type SET /? For all reference).

Secondly, check the goto stream - this never returns to A.

Third, check the syntax of the if ( != Does not exist in the package).

+3
source

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 
+3
source
 @echo off set a=0 :Count set /aa=a+1 echo %a% goto Count 
0
source

try it:

 @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)); 
0
source

All Articles