【问题标题】:Windows 7 Batch For Loop with If arguement带有 If 参数的 Windows 7 批处理 For 循环
【发布时间】:2013-04-04 05:12:58
【问题描述】:

我正在尝试编写一个批处理来在一夜之间执行一个脚本,该脚本要到深夜才会创建。如果路径存在,我希望循环总共检查 12 次。如果它确实存在,那么我希望它启动脚本测试。如果它不存在,那么我希望它休眠一个小时并再次检查。我还希望循环告诉我它每次循环了多少次。这是我所拥有的......

@Echo off
cls
set build = %1
set counter = 1
for /l %%a in (1,1,12) do (
if exist {%build%} (
goto StartTest
        )
set Timer = %counter% + %Timer%
echo build does not exist. %Timer% out of 12 hours left for the build path to exist or this script will exit.
Sleep 3600000
)

goto end

:StartTest
Echo Starting Tests...

:end
Echo Times up, build path wasn't made

So far this has been the output in cmd:
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
build does not exist.  out of 12 hours left for the build path to exist or this
script will exit stop trying.
'Sleep' is not recognized as an internal or external command,
operable program or batch file.
Times up, build path wasn't made

C:\Users\james\Desktop>

注意它说sleep 是一个无法识别的命令,而%Timer% 是空白的

【问题讨论】:

  • 查看here 了解如何在批处理文件中休眠。进行算术运算时需要使用SET /A,即set /A Timer = %counter% + %Timer%
  • 您是否尝试过使用timeout

标签: windows windows-7 batch-file


【解决方案1】:
set build = %1
set counter = 1

和类似的可能是您的问题的根源。

空格很重要。例如,set build = %1 行设置变量“build” - 带有空格,而不是“build”,设置的值将是“%1”,而不是“%1” - 第一个参数但 WITH前导空格。

SLEEP 不是标准的可执行文件。试试TIMEOUT(执行

timeout /?

从文档提示中。

并且TIMER 没有在任何地方设置 - 它将被 [nothing] 替换


附录:几个演示程序

@ECHO OFF&SETLOCAL&cls
ECHO --- a little demonstration of SET syntax
@ECHO on
SET problem=this is fine
SET problem =this is a problem
@ECHO OFF
ECHO.
ECHO Result:
ECHO.
SET proble|FIND /i "problem"
ECHO.
ECHO See how the space is included in the variable NAME
ECHO so that there are two distinct variables?




@ECHO off&setlocal&CLS
ECHO Demonstrating the use of %%var%% IN a block
ECHO.
SET var=Original value
ECHO Before the block, %%var%%=%var%
FOR %%i IN (1 2 3) DO (
  SET var=New value %%i
  ECHO loop %%i : %%var%%=%var%
  )
ECHO After the block, %%var%%=%var%
ECHO.
ECHO BECAUSE the block is first PARSED, then executed.
ECHO in the parsing process, %%var%% is replaced by its
ECHO value as it stood when the block was parsed - BEFORE execution
ECHO.
ECHO now try using a SETLOCAL ENABLEDELAYEDEXPANSION command first:
ECHO.
SETLOCAL ENABLEDELAYEDEXPANSION
SET var=Original value
ECHO Before the block, %%var%%=%var% and ^!var^!=!var!
FOR %%i IN (1 2 3) DO (
  SET var=New value %%i
  ECHO loop %%i : %%var%%=%var% BUT ^^^!var^^^!=!var!
  )
ECHO After the block, %%var%%=%var% and ^^^!var^^^!=!var!
ECHO.


修改以演示使用 CALL 以实现与 ENABLEDELAYEDEXPANSION 相同的最终结果
@ECHO OFF
SETLOCAL
SET count=0
FOR /l %%i IN (1,1,10) DO (
  SET /a count+=1
  CALL ECHO Loop %%i: count=%%count%%
)

这里发生的是解析器用于检索COUNT 的当前(即运行时)值。它所做的实际上是将%%count%% 替换为%count%,因为% 转义%,因此它将第二个% 视为普通字符,没有特殊含义。因此,ECHO Loop 3: count=%count% 行(例如)在CALL 中执行,%count% 被 THEN-current 环境中的解析器适当地替换。

还要注意SET/A 语句。 SET/A 是对SET 的后续添加,并将表达式的算术结果分配给变量 - 转换回字符串,因为所有环境变量都是字符串。我使用的语法是 C 风格的表达式,用于将 1 添加到目标。它同样可以表示为SET /A count=%count% + 1 甚至set /a count = count + 1,因为第一个表达式将被解析为(例如)SET /a count=3 + 1 并且SET/A 的语义使得空格(不是数字)在整个过程中被忽略声明的评估。

因此,

set count=0
set /a count = 0

两者都实现相同的目标,但仅在使用 /a 选项时。

【讨论】:

    【解决方案2】:

    我最终选择了迄今为止有效的东西。我只是还没弄清楚如何在那个 for 循环中添加一个计数器。你说这是因为我在counter=1之间有一个空格对吗?

    这是我用过的:

    @Echo off
    if "%1"=="" goto Error1
    for /l %%a in (1,1,12) do (
    set build=%1
    if exist "%build%" goto StartTest
    echo %build% does not exist...
    echo Will try again in 1 hour
    echo.
    echo.
    echo.
    timeout /t 3600 /nobreak > NUL
    )
    
    goto endfail
    
    :StartTest
    Echo Starting Tests...
    "D:\Program Files (x86)\Mozilla Firefox\firefox.exe" -file "C:\Selenium\Nightly.html"
    
    goto end
    
    :Error1
    Echo Error: Invalid Parameter
    Echo NightlyTest.bat "Build_Path"
    goto end
    
    :endfail
    Echo Times up, build path wasn't made
    
    :end
    

    【讨论】:

      【解决方案3】:

      对于重试(我不知道为什么我没有早点想到这个),我可以在循环中添加 echo %%a out of 12 tries 行。

      【讨论】:

      • for /l %%a in (12,-1,1) do ( 也是合法的。我将在我的回复中添加一个小修改,以显示您如何可以在原始代码中使用count - 但首先要先迈出第一步。对您使用的变量充满信心。然后是更高级的技术。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多