【问题标题】:How do I make something loop 10 times then continue in batch?如何使某些内容循环 10 次然后批量继续?
【发布时间】:2015-03-31 19:38:05
【问题描述】:

批量,我想运行 10 次然后goto continue

我该怎么做?

【问题讨论】:

  • 你试过什么?如果您需要一些起点,我建议您查看来自for /? 的文档。
  • @SomethingDark 我试过了:e set count=%count%+1 if %count% GEQ 10 goto e

标签: loops batch-file


【解决方案1】:

这是一种方法:

@echo off
setlocal enabledelayedexpansion

echo before

set i=0
:loop1

echo something 10 times

set /a i+=1
if !i!==10 goto continue

goto loop1
:continue

echo after

exit /b 0

【讨论】:

    【解决方案2】:

    最简单的方法是使用for /L 循环。

    for /L %%A in (1,1,10) do (
        rem your code here
    )
    goto continue
    

    for /L 循环的格式为 (start,step,end),因此 (1,1,10) 表示从 1 开始,以 1 秒计数到 10。

    【讨论】:

    • 如果我像 for /L %%A in (1,1,10) do ( set randomfile=%RANDOM% echo xfileav%RANDOM%to%randomfile% > %randomfile% copy %randomfile% %vdefdir% del "%randomfile%" ) 那样使用它会出现语法错误
    • @MarkieJonesWTF - 代码在 cmets 中显示不正确,所以我只是猜测你在哪里放置换行符,但你可能需要 setlocal enabledelayedexpansion 并使用 !randomfile! 而不是 @987654330 @ 因为变量是在代码块内部创建的。
    猜你喜欢
    • 2023-03-19
    • 2012-04-03
    • 1970-01-01
    • 2017-08-19
    • 2012-10-18
    • 1970-01-01
    • 2023-01-07
    • 1970-01-01
    • 2015-09-23
    相关资源
    最近更新 更多