Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • DSA
  • Practice Problems
  • Python
  • C
  • C++
  • Java
  • Courses
  • Machine Learning
  • DevOps
  • Web Development
  • System Design
  • Aptitude
  • Projects
Open In App
Next Article:
How to Create a Batch File in Windows?
Next article icon

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
How to Create a Batch File in Windows?
author
_Krish
Improve
Article Tags :
  • TechTips
  • Batch-script

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences