[Bat] How to make a batch file that loops infinitely!Let's process it many times using the label!


Hello!This time, I will introduce one of the basics of batch files, an example of creating a batch that loops processing infinitely.

Batch files are a very useful technique when trying to manage Windows, but even if you just read the material (reference), you will not be able to learn it.

This time we will use a function called a label, but you can use it for various purposes with a little ingenuity.
As an example, let's make an infinite loop ♪

Thing you want to do

I want to make an infinite loop using labels.

Let's count the number of loops.

Script example

@echo off
set kaisuu=0


set /a kaisuu=%kaisuu%+1
echo %kaisuu%回目です。

goto :Label


I would like to briefly explain the contents.

Loop with label

You can force a jump to the label of the batch file during execution.
If you make a line with ": label name", you can skip to it with "goto label name".

goto: Label What you want to skip: Label

Normally, it is used to skip the processing in the middle when testing a batch file, or to collect the processing in advance and return it to the original position (: subroutine). (Actually you should use Call and Exit / b)

However, you can skip it not only to the back of the process, but also to the front.
You use this to create an infinite loop.

: Label Infinite loop processing goto: Label

Calculation using the set command

By the way, I'm counting the number of loops, but at this time I use the "Set / a" command.

In a batch file, you have to use the commands provided for simple calculations.
The reason is that it doesn't automatically give the command to "use the calculation function".

Therefore, you have to clearly write the command "Set / a", which says "Save the calculation result after performing the calculation".

set / a kaisuu =% kaisuu% + 1 echo% kaisuu% This is the time.

Therefore, it looks like this.
By the way, if you enclose the variable kaisuu in %%, it represents the value of the contents, and if you do not enclose it, it represents the container of the value.
When calculating, the contents are used, so enclose it in %%, and the calculation result is put in a container, so% is not required.


This time, I introduced an example of creating an infinite loop with a batch file.
It's a fairly basic part, but I had a few questions, so I wrote it as an answer.

Labels and goto commands are very simple, but there are many tricky ways to use them.
I think one of them is to fly to the front side with goto.

Can this be done?I think that you will learn it as you try various things, so I think it is good to try various things.

Is the screen that prompts you to enter a password etc. often used in this infinite loop?
It is an impression that is often used for Yes / No interactive input.

: input
set / p PASSWORD = "password =" set / p PASSWORD = "Please enter again =" if% PASSWORD% neq% PASSWORD_C% (echo input is incorrect pause cls
   goto: input
) echo Input OK pause

It's also often used to monitor commands that you want to execute all the time or until a specific process is completed.
It can be used for various purposes depending on the idea, so I hope you can make use of this.

I hope it helps you.


Translate »
I copied the title and URL