How to Create an Infinite Loop in Windows Batch File?
An infinite loop in Batch Script refers to the repetition of a command infinitely. The only way to stop an infinitely loop in Windows Batch Script is by either pressing Ctrl + C or by closing the program.
Syntax: Suppose a variable ‘a’
:a your command here goto a
Here, you need to know how to create a batch file in windows. It is very simple. First, copy the code in a notepad file and save this file with .bat extension. To run or execute the file, double click on it or type the file name on cmd.
Example 1: Let’s start by looping a simple command, such as ‘echo’. ‘echo‘ commands is analogous to ‘print’ command like in any other programming languages. Save the below code in a notepad file like sample.bat and double click on it to execute.
@echo off :x echo Hello! My fellow GFG Members! goto x
To stop this infinite loop, press Ctrl + C and then press y and then Enter.
Example 2: Suppose we want to loop the command ‘tree’. ‘tree’ command pulls and shows directory and file path in the form of a branching tree.
@echo off REM turning off the echo-ing of the commands below color 0a REM changing font color to light green cd c:\ REM put the directory name of which you want the tree of in place of c :y REM you can add any other variable in place of y tree goto y
Note: ‘REM’ command is only used for typing comments in the batch script program, you can ignore them while typing the program. They are only put for the understanding of the program script and have no real use in the program. Here you can see the below option also.
@echo off color 0a cd c:\ :y tree goto y