首页 文章

如何在Windows批处理文件中创建无限循环?

提问于
浏览
133

这基本上是我想要的批处理文件 . 每当我按任意键超过“暂停”时,我希望能够重新运行“Do Stuff” .

while(true){
    Do Stuff
    Pause
}

看起来只有 for 循环可用且批量没有 while 循环 . 那么如何创建一个无限循环呢?

6 回答

  • 0

    如何使用好(?)旧goto

    :loop
    
    echo Ooops
    
    goto loop
    

    另请参阅this以获取更有用的示例 .

  • 24

    一个非常无限的循环,从1到10计数,增量为0 .
    你需要无限或更多的增量来达到10 .

    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
    

    如果你需要一个非常无限的循环,这可能很有用,因为它比 goto :loop 版本快得多,因为for循环在启动时完全缓存一次 .

  • 13

    one-line command 中的无限循环用于 cmd 窗口:

    FOR /L %N IN () DO @echo Oops
    

    enter image description here

  • 246

    阅读 help GOTO

    并尝试

    :again
    do it
    goto again
    
  • 3

    另一种更好的方法:

    :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
    

    这是我在Windows上需要将任何消息加密成md5哈希时所使用的简单批处理(需要openssl),除了给定Ctrl C或空输入外,程序将忠实重复 .

相关问题