Open In App

How to Create an Infinite Loop in Windows Batch File?

Last Updated : 12 Mar, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Windows batch files are a powerful tool for automating repetitive tasks, managing system operations, and streamlining workflows. But what happens when you want to create a process that runs indefinitely?

In this blog post, we’ll walk you through the steps to create an infinite loop in a Windows batch file, explain how it works, and provide practical examples to help you implement it in your scripts.

What is Windows Infinite Loop in Windows Batch File

An infinite loop in a Windows batch file is a programming construct where a set of commands repeats indefinitely without stopping. This is achieved by creating a loop that has no exit condition or relies on an external trigger to terminate. Infinite loops are often used in scenarios where a process needs to run continuously, such as monitoring systems, repetitive tasks, or waiting for specific events.

How It Works:

In a Windows batch file, an infinite loop is typically created using the goto command or the for loop with a condition that always evaluates to true. Since there’s no built-in “while” loop in batch scripting, these methods are commonly used to achieve continuous execution.

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 infinite 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 the .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’. The ‘ echo ‘ command is analogous to the ‘print’ command like in any other programming language. 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

Output:

Infinite in Windows Batch Script

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’. The ‘tree’ command pulls and shows the 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

Output:

Infinite Tree Command Loop in Windows Batch Script

Conclusion

Creating an infinite loop in a Windows batch file can be useful for a variety of tasks, such as automating repetitive actions or continuously monitoring a process. An infinite loop is a loop that keeps running until it’s manually stopped, making it a powerful tool in scripting. This guide will walk you through how to create an infinite loop in a Windows batch file using simple commands. Whether you’re new to scripting or just need a quick refresher, you’ll find this tutorial easy to follow and apply.



Next Article
Article Tags :

Similar Reads