views:

68

answers:

2

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?

A: 

Try Exit instead of Quit

Traveling Tech Guy
Only half the answer. This will kill your session instantly which is often not what you want when working on the command-line :-)
Joey
+4  A: 

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)"
Joey