Creating a While Loop in Batch | Batch Basics

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Creating a While Loop in Batch | Batch Basics

#1 Post by PaperTronics » 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:

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

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Creating a While Loop in Batch | Batch Basics

#2 Post by elzooilogico » 23 Aug 2017 06:07


ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Creating a While Loop in Batch | Batch Basics

#3 Post by ShadowThief » 23 Aug 2017 06:53


PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Creating a While Loop in Batch | Batch Basics

#4 Post by PaperTronics » 24 Aug 2017 05:17

@elizooilogico - Nice relevant post that you linked, I wasn't able to read the whole post due to lack of time, but I will in the future.

@ShadowThief - I wasn't able to get your intentions clearly by that link you provided? What did you want to show?



PaperTronics

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Creating a While Loop in Batch | Batch Basics

#5 Post by ShadowThief » 24 Aug 2017 06:56

"If," "goto", and "while" should all be the same color because they're all commands.

This advice extends to your website as well.

PaperTronics
Posts: 118
Joined: 02 Apr 2017 06:11

Re: Creating a While Loop in Batch | Batch Basics

#6 Post by PaperTronics » 25 Aug 2017 14:42

ShadowThief wrote:"If," "goto", and "while" should all be the same color because they're all commands.

This advice extends to your website as well.


Ah, thanks for pointing that out!

But you're wrong about the advice on my website since I and kvc have changed the layout completely! The article design is also fixed thanks to your advice back in February. Have a look at it again and I guarantee that you won't regret it!



PaperTronics

Post Reply