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
- How to create a Folder without any name and icon in Windows OS?
- How to Create and Configure Wi-Fi Hotspot in Windows 10?
- How to Create Partitions of a Hard Drive in Windows 10?
- Zip files in Windows
- How to use Emojis in Windows 10?
- What is Robocopy in Windows?
- Windows Shortcut Keys
- IDE for Python programming on Windows
- Useful CMD commands for daily use in Windows OS
- How to Boost Performance in Windows 10?
- How To Use Bash Shell Natively On Windows 10
- Uninstall Linux completely from a PC with Windows
- How to mount windows drives in Ubuntu
- Setup GitLab Repository On Windows 10
- How to Change Colours in Command Prompt in Windows?
- Autorun a Python script on windows startup
- Windows Server 2012 R2 Stuck at "Updating Your System”
- Creating a Dual Boot System with Linux and Windows
- Hackathon - Think, Code, Create
- Write a bash script to print a particular line from a file
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.