Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Counter" in Batch

Tags:

batch-file

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.

like image 488
Wolverine1621 Avatar asked Mar 16 '13 13:03

Wolverine1621


People also ask

What is a batch counter?

A Counter that functions as a resetting counter, but that also counts the number of times the counter's set value is reached. The status of the batch output is retained until the batch counter reset signal is input.

How do you comment in a batch script?

Comments Using the :: Statement The other way to create comments in Batch Script is via the :: command. Any text which follows the :: statement will be treated as comments and will not be executed. Following is the general syntax of this statement.

Which of this is termed as a batch file?

A batch file is a script file that stores commands to be executed in a serial order. It helps automate routine tasks without requiring user input or intervention. Some common applications of batch files include loading programs, running multiple processes or performing repetitive actions in a sequence in the system.


1 Answers

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
like image 128
A. Rodas Avatar answered Sep 20 '22 16:09

A. Rodas