Question

Does anyone know how to break out of a for loop when it's typed directly into the windows command-line? I know you can use gotos and labels to break out of it when it's in a batch file, but I can't find anything about breaking out of one on the command line. Here's a simple example:

C:> for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & QUIT)

This should infinitely ping google.com. If it ever fails, it beeps (echo ^G), displays a message box to the user "user" that says "Google is down!", and QUITs. I don't know how to do the quit part though. I guess I could do something like taskkill /f /im cmd.exe, but I was looking for something more elegant. Any tips?

Was it helpful?

Solution

Wrap it into another instance of cmd and use exit:

cmd /c "for /l %i in (1,0,1) do @ping -n 1 google.com || (echo ^G & msg user "Google is down!" & exit)"

OTHER TIPS

Try Exit instead of Quit

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top