for /L %%n in (1,0,10) do (
echo do stuff
rem ** can't be leaved with a goto (hangs)
rem ** can't be stopped with exit /b (hangs)
rem ** can be stopped with exit
rem ** can be stopped with a syntax error
call :stop
)
:stop
call :__stop 2>nul
:__stop
() creates a syntax error, quits the batch
:LOOP
timeout /T 1 /NOBREAK
::pause or sleep x seconds also valid
call myLabel
if not ErrorLevel 1 goto :LOOP
这样你也可以处理错误
55
以下是使用循环的示例:
echo off
cls
:begin
set /P M=Input text to encode md5, press ENTER to exit:
if %M%==%M1% goto end
echo.|set /p ="%M%" | openssl md5
set M1=%M%
Goto begin
6 回答
如何使用好(?)旧goto?
另请参阅this以获取更有用的示例 .
一个非常无限的循环,从1到10计数,增量为0 .
你需要无限或更多的增量来达到10 .
如果你需要一个非常无限的循环,这可能很有用,因为它比
goto :loop
版本快得多,因为for循环在启动时完全缓存一次 .one-line command 中的无限循环用于
cmd
窗口:阅读
help GOTO
并尝试
另一种更好的方法:
这样你也可以处理错误
以下是使用循环的示例:
这是我在Windows上需要将任何消息加密成md5哈希时所使用的简单批处理(需要openssl),除了给定Ctrl C或空输入外,程序将忠实重复 .