Say, you want to run the command “echo Hello, World” repeatedly forever. A goto command can be used to create a forever loop.
1 2 3 4 5 6 7 |
@echo off :loop echo Hello, World ping 127.0.0.1 -n 5 > nul goto loop |
Using ping here is to add 5-second sleep in the loop since there is no built-in command for sleeping. A little sleep may be required in order to break the execution if necessary. If the command to be run throws an exception, the script will stop. To ignore exceptions and continue the loop, we can run the command usingĀ start.
1 2 3 4 5 6 |
@echo off :loop start "" /wait cmd.exe /c "echo Hello, World & ping 127.0.0.1 -n 5 > nul" goto loop |