How to Create an Infinite Loop in Windows Batch File?
Last Updated : 12 Mar, 2025
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:

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:

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.
Similar Reads
How to Create a Batch File in Windows?
A batch file is a straightforward text document in any version of Windows with a set of instructions meant to be run by Windows' command-line interpreter (CMD) by maintaining all the functions. Batch files are a great tool for streamlining internally configured processes, automating tedious jobs, an
5 min read
How to Merge/Hide One File into Another Using CMD in Windows?
Merging or hiding files within other files using Command Prompt in Windows is a clever technique for managing and protecting your data. Whether you want to consolidate multiple files into one or conceal sensitive information, CMD offers powerful commands to achieve this seamlessly. Learning how to h
3 min read
How to Create a File in CMD
We all know that one can easily create files using GUI options, but what if you can use a faster way to create files through the command-line interface? This is not a necessary but useful skill for anyone working with Windows. Whether you're automating tasks, working on scripts, or preferring using
5 min read
How to Batch Rename Images (or other Files) on Windows?
Renaming multiple files individually can be a time-consuming task, especially when you have a large number of images or other files to manage. Fortunately, Windows offers several methods to batch rename files efficiently. Whether you need to batch rename images on Windows or organize other types of
5 min read
How to Create a Bootable USB Flash Drive in Windows Using CMD
Want to install new Windows on your computer using a USB drive and looking for a way to create a bootable USB? Then you can do it with a simple tool built into your computer called CMD (Command Prompt), and you donât need any extra software. This guide will show you 12 easy steps to turn your USB in
5 min read
How to List all Files in a Directory using CMD
Struggling to find or organize files buried in folders on your Windows PC? While File Explorer offers a visual way to browse, the Command Prompt (CMD) provides a faster, more powerful method to list, sort, and manage files, especially when dealing with large directories or automating tasks. In this
5 min read
How to Create a Virus to Open Up Multiple Window Automatically?
The virus that we are going to create in this article will automatically open up multiple windows in the victim's Windows PC. This is an annoying type of Virus because as per the heading, this is capable of opening multiple window screens in front of the user without allowing the user to open any ot
2 min read
How to Create a Virus that Deletes the Registry File of Windows OS?
The virus that we are going to create in this article can be used to wipe out the Windows registry or simply registry from the database of the Windows system. The registry or Windows registry is a database that holds information regarding the settings, options, and critical values that software and
2 min read
How to use CMD for Python in Windows 10?
Using the Command Prompt (CMD) is an effective way to interact with Python on your Windows 10 machine. Whether you're writing scripts, testing code, or running programs, mastering CMD for Python in Windows 10 is crucial. This article will guide you on how to run Python in CMD, execute scripts, and t
4 min read
How to Select Multiple Files on Windows?
Quick Preview : How to Select Multiple Files on Windows?How to Select Multiple Files on Windows?Here is a short preview of our active and speedy learner. Follow these steps to how to select multiple files in windows. Click on the first file.Hold down "Shift" key.Click on the last file.All files in b
5 min read