我有一个简单的批处理文件。你可以举任何一个例子。我怎样才能让批处理文件从头到尾不断循环运行,直到达到指定的时间或达到计数器的值?
答案1
如果你正在Windows Vista或更高版本,这里有一些可以满足您的需求的东西。例如,这个批处理文件运行一个进程,最多执行 11 次,或者直到 04:17(以先到者为准),在每个进程之间等待一秒钟(不占用 CPU——如果您有文件处理,您可能不需要这个,或者您可能实际上想要占用 CPU):
@echo off
echo Starting Batch...
REM Use 'setlocal' so that we can use an environment variable without it leaking out to the caller.
setlocal
REM Initialize the counter to zero
set counter=0
:AGAIN
REM Increment the counter
set /A counter+=1
echo Processing loop %counter%...
REM Wait for one second so as not to peg the CPU
timeout /T 1 /NOBREAK>nul
REM Check the counter to see if we've done enough iterations--bail out if we have
if '%counter%'=='11' goto END
REM Check the time to see if the target time has been reached--if not go back and process again
FOR /F "TOKENS=1 eol=/ DELIMS=/ " %%A IN ('time /T') DO IF NOT '%%A'=='04:17' goto AGAIN
:END
REM End the local processing and go back to the main environment
endlocal