我有一个批处理文件,我想向用户显示该文件中的进程。我已经创建了包含Operation和Status标头的小表。每个表都用于特定的命令/行。在该行运行后,我打印相应的表,清除屏幕并打印具有更新状态的新表。所以你看起来很好看。这是代码:
@echo off
REM Process started
goto zeroth
timeout /t 1 /nobreak > NUL
REM do some work. first operation is done
goto first
timeout /t 1 /nobreak > NUL
REM do some work. second operation is done
goto second
REM ...
:zeroth
cls
echo Operation Status
echo ====================================================
echo * op1 Waiting
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
:first
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
:second
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
pause
但它不起作用。我已经检查了其他标签示例,他们通常最后有goto end
或者其他东西,但我不打算关闭窗口。我应该修改什么?
答案 0 :(得分:0)
goto
非常简单,脚本从上到下阅读,一旦下来,他就不会回去,除非邀请他们。 goto
命令是一种方法。
例如:
@echo off
echo goto is also used to skip a step, example go directly to step-2.
@timeout /t 2 /nobreak>NUL
goto step-2
:step-1
echo hello world
@timeout /t 2 /nobreak>NUL
goto step-3
:step-2
echo How are you? Oh sorry, I missed Salute.
@timeout /t 2 /nobreak>NUL
goto step-1
:step-3
echo Show me your code
@timeout /t 2 /nobreak>NUL
type %~f0 | more
exit /b 0
编辑:
使用call
修改了同样的示例,以便在goto :eof
@echo off
echo goto is also used to skip a step, example go directly to step-2.
@timeout /t 2 /nobreak>NUL
goto step-2
:step-1
echo hello world
@timeout /t 2 /nobreak>NUL
::goto step-3
goto:eof
:step-2
echo How are you? Oh sorry, I missed Salute.
@timeout /t 2 /nobreak>NUL
::goto step-1
call :step-1
:step-3
echo Show me your code
@timeout /t 2 /nobreak>NUL
::type %~f0 | more
exit /b 0
答案 1 :(得分:0)
您可以将批处理文件视为操作系统需要运行的CMD命令列表,以及运行它们的顺序。与其他脚本语言一样,批处理文件从上到下运行,除非方向由goto
语句及其对应的行标签更改。
在您的情况下,goto zeroth
转到:zeroth
标签,然后继续沿着文件底部的快捷方式。因为它永远不会被告知在脚本中更高,所以它永远不会。
幸运的是,如果将goto
替换为call
,您还可以批量使用标签来模拟子例程。 (它实际上不是一个子程序;它更像是将一块脚本作为自己独立的批处理脚本运行。)当你这样做时,你还必须使用goto :eof
告诉脚本“子程序”结束的位置。
@echo off
REM Process started
call :zeroth
timeout /t 1 /nobreak > NUL
REM do some work. first operation is done
call :first
timeout /t 1 /nobreak > NUL
REM do some work. second operation is done
call :second
REM ...
pause
REM We are at the end of the main chunk of code; everything below this is a subroutine
exit /b
:zeroth
cls
echo Operation Status
echo ====================================================
echo * op1 Waiting
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
goto :eof
:first
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
goto :eof
:second
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
goto :eof
答案 2 :(得分:0)
您的原始代码存在问题,第4行您有goto,这很好
@echo off
REM Process started
goto zeroth
所以代码进入" zeroth"正如它应该。但是在结束时:第0:你没有任何代码告诉批处理文件去其他任何地方,所以它继续下降。使用goto
是跳转批处理文件的好方法。但是,如果你只是下线,IE转到0,1,2,3 ......你根本不需要goto
,你可以简单地删除所有的gotos和代码运行得很好。
@ECHO OFF
REM Process started
goto zeroth
:zeroth
cls
echo Operation Status
echo ====================================================
echo * op1 Waiting
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
TIMEOUT>NUL /T 1
::zeroth done, going to next
goto first
:first
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
TIMEOUT>NUL /T 1
:: first done, going to next
:second
cls
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
echo.
pause
:: Processes all done
GOTO :EOF
答案 3 :(得分:0)
使用SomethingDark的answer,我将代码更改为:
@ECHO OFF
call :1stOpRunning
timeout /t 1 /nobreak > NUL
REM 1st op code
call :2ndOpRunning
timeout /t 1 /nobreak > NUL
REM 2nd op code
call :3rdOpRunning
timeout /t 1 /nobreak > NUL
REM 3rd op code
call :4thOpRunning
timeout /t 1 /nobreak > NUL
REM 4th op code
call :allDone
exit /b
:1stOpRunning
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Waiting
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
timeout /t 1 /nobreak > NUL
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Processing
echo * op2 Waiting
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
goto :eof
:2ndOpRunning
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Processing
echo * op3 Waiting
echo * op4 Waiting
echo ====================================================
goto :eof
:3rdOpRunning
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Processing
echo * op4 Waiting
echo ====================================================
goto :eof
:4thOpRunning
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Done
echo * op4 Processing
echo ====================================================
goto :eof
:allDone
cls
echo.
echo Operation Status
echo ====================================================
echo * op1 Done
echo * op2 Done
echo * op3 Done
echo * op4 Done
echo ====================================================
echo.
echo Press any key to continue . . .
timeout /t 5 > NUL
exit