ad

Sunday, 3 January 2016

How to loop in batch programing

What is loop and how it works

Loopping is when you need to do a certain task again and again.

Lets say your friend's been pissing you off lately and you decide to annoy him by creating a little prank virus that open a program again and again until the process is terminated.

Lets start, lets open notepad once.
You can open any program in your computer by using start command. 'start' command executes a program that's in front of it. You can also use full path to that program. If you save following code in batch file and run it you will see notepad opening.
start notepad.exe


Goto loop

Go to loop uses label to repeat itself. What's inside loop commands will execute. Having notepad.exe inside 'goto' loop will make notepad.exe open again and again.

To start loop we define a label. Label's a part of code where loop starts, and goto is a command that tells CMD to go back to where the label is. Let me give an example to make it clear:

1. We define ':label'. Note label must have ':' color before it's name, and it can be named anything.
2. We tell it what to do while it's looping(loop body)
3. We tell it to go back to label.

:thisIsLabel
echo hi
goto thisIsLabel



Above code will echo 'hi' until CMD is closed.

Same thing can be done for our little notepad.exe prank. We replace 'echo hi' command with our start command and name of program that you'd want to open.

Back to batch basics

No comments:

Post a Comment