如何将批处理脚本循环一定次数(x10或其他)? 如果代码是:
@echo off
:loop1
Start taskmgr.exe
Goto loop
:loop2
Start cmd.exe
goto loop2
如何循环loop1几次并转到loop2?
任何有用的答案将不胜感激:)
答案 0 :(得分:3)
如果打开命令窗口并输入FOR /?它会给你你正在寻找的命令。
FOR /L %variable IN (start,step,end) DO command [command-parameters]
The set is a sequence of numbers from start to end, by step amount.
So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would
generate the sequence (5 4 3 2 1)
答案 1 :(得分:2)
由于我忽略的原因,FOR
命令不能用于循环特定标签。
例如(我可能错了):
@echo off
for /L %%a in (1,1,2) do (
goto loop
)
:loop
echo this won't loop for 2 times.
这将简单地循环无限次。所以,我已经找到了一种替代的简单方法来循环标签多次。为此,我创建了一个变量,如loop
,每次标签重复时都会有更大的数字。
有一个例子:
@echo off
set loop=0
:loop
echo hello world
set /a loop=%loop%+1
if "%loop%"=="2" goto next
goto loop
:next
echo This text will appear after repeating "hello world" for 2 times.
输出:
hello world
hello world
This text will appear after repeating "hello world" for 2 times.
说明:
set loop=0
将变量loop
的值设置为0; set /a loop=%loop%+1
时,:loop
都会加1。if "%loop%"=="2" goto next
测试变量loop
是否等于2
(因此它重复了2次);如果它相等,它将转到标签:next
,否则它将转到标签:loop
。我希望这会对你有所帮助,
LuigiGrande1
答案 2 :(得分:1)
以下是一个例子:
@echo off
for /L %%a in (1,1,10) do (
Start taskmgr.exe
)
for /L %%a in (1,1,10) do (
Start cmd.exe
)
答案 3 :(得分:1)
@echo off
set /a a=1
goto loop
:loop
echo looped %a% times so far
set /a a=%a%+1
if %a%=10 (
echo looped a total of %a% times
)
您也可以使用以下命令:
@echo off
for /l %%a in (1,1,10) do (
rem %%a is a variable and starts at a value of 1, steps by 1 and ends at a value of 10
start "Command Prompt #%%a Opened" cmd
rem eg Command Prompt #1 Opened
rem eg Command Prompt #2 Opened
rem etc
)
答案 4 :(得分:0)
试试这个:
@echo off
set loopvar=1
:repeat
if %loopvar% gtr 5 (goto :done) else (set /a loopvar=%loopvar%+1 && echo Loop && goto :repeat)
:done
在 :done 下,编写您希望它在循环后执行的代码。确保将 5 替换为您希望循环重复的次数。 对于循环中的延迟,试试这个:
@echo off
set loopvar=1
:repeatwithdelay
if %loopvar% gtr 5 (goto :done) else (set /a loopvar=%loopvar%+1 && timeout /T 1 >nul && echo Loop && goto :repeatwithdelay)
希望有帮助!