Creating a While Loop in Batch | Batch Basics
Posted: 23 Aug 2017 05:15
If you have knowledge of programming languages other than Batch, you may know what is a WHILE loop. A While loop is basically a loop which has similar functionality to the FOR loop.
As you might know, there is no WHILE loop in Batch. But we can create a While loop in Batch using very Simple but Effective Batch Programming.
Creation
We can easily create a WHILE loop in Batch by mixing together IF conditions, the GOTO command and some plain old Batch logic.
Below is demonstration of a very basic WHILE loop in Batch:
This code will print the text "I am Running [value of count variable] time" ten times on the screen and then print "I have finished my journey". This was a very basic WHILE loop. There are countless other creative methods you can utilize this WHILE loop!
If you have any suggestions or questions I will be pleased to answer them!
PaperTronics
As you might know, there is no WHILE loop in Batch. But we can create a While loop in Batch using very Simple but Effective Batch Programming.
Creation
We can easily create a WHILE loop in Batch by mixing together IF conditions, the GOTO command and some plain old Batch logic.
Below is demonstration of a very basic WHILE loop in Batch:
Code: Select all
@echo off
Set count=0
:a
if %count% gtr 10 (goto :b) else (set /a count+=1)
Echo I am Running %count% time
goto :a
:b
Echo I have finished my journey
pause
exit /b
This code will print the text "I am Running [value of count variable] time" ten times on the screen and then print "I have finished my journey". This was a very basic WHILE loop. There are countless other creative methods you can utilize this WHILE loop!
If you have any suggestions or questions I will be pleased to answer them!
PaperTronics