带有If参数的Windows 7 Batch For循环

时间:2013-04-04 05:12:58

标签: windows windows-7 batch-file

我正在尝试编写一个批处理,以便在一夜之间执行一个脚本,直到深夜都不会创建。如果路径存在,我希望循环检查总共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%是空白的

3 个答案:

答案 0 :(得分:2)

set build = %1
set counter = 1

和类似的可能是你的问题的来源。

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

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

TIMEOUT

来自文档提示。

并且timeout /? 未设置在任何位置 - 它将被[nothing]

替换

补遗:几个示范程序

TIMER

<小时/> 证明使用@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. 达到与ENABLEDELAYEDEXPANSION相同的最终结果的修正案

CALL

这里发生的是解析器用于检索@ECHO OFF SETLOCAL SET count=0 FOR /l %%i IN (1,1,10) DO ( SET /a count+=1 CALL ECHO Loop %%i: count=%%count%% ) 的CURRENT(即运行时)值。它的作用实际上是将COUNT替换为%%count%%,因为%count%会转义%,因此它将第二个%视为一个有序的角色,没有特殊含义。因此,行(例如)%ECHO Loop 3: count=%count%中执行,%count%由THEN-current环境中的解析器适当地替换。

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

因此,

SET/A

两者都达到了相同的目标,但只有在使用set count=0 set /a count = 0 opion时才会实现。

答案 1 :(得分:0)

我最终选择了迄今为止有用的东西。我还没想出如何在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

答案 2 :(得分:0)

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