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 Create a File in CMD Creating a file using Windows GUI is very easy, but did you know that you can also create files using Windows CMD? Now, this article will walk you through the steps to create a file using CMD, ensuring that you can efficiently manage your files directly from the command prompt.How to Create a File i
4 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 oth
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 Automate Disk Cleanup Using Task Scheduler in Windows? To keep your Windows system operating at its best, you must remove any superfluous files from it. Using Task Scheduler to automate disk cleanup is one of the simplest ways to make sure it happens regularly. By automating this repetitive task, you may avoid manually cleaning your computer and save ti
4 min read
How to Use Command Prompt - Windows Command Prompt Guide Learn how to manually run the Command Prompt in different versions of Windows - The Windows Command Prompt (CMD) is a powerful tool that allows users to execute commands, automate tasks, and troubleshoot system issues efficiently. Whether youâre a beginner looking to learn basic CMD commands or an a
5 min read
How to Copy Files from One Directory to Another Using CMD Have you ever needed to copy a bunch of files from one folder to another without clicking through endless windows? Or perhaps youâre looking to streamline a repetitive task with a single line of text? If so, the Command Prompt (CMD) on Windows is here to save the day! Copying files between directori
5 min read
How to Delete a File or Folder Using CMD? When working with files on Windows, File Explorer usually makes creating and deleting files simple. But what if File Explorer freezes or stops responding? In such cases, the Command Prompt (CMD) offers a reliable alternative. CMD allows you to create and delete files or folders using simple commands
4 min read
Writing a Windows batch script In Windows, the batch file is a file that stores commands in a serial order. The command line interpreter takes the file as an input and executes in the same order. A batch file is simply a text file saved with the .bat file extension. It can be written using Notepad or any other text editor. A simp
3 min read