0

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

4

3 回答 3

2
set build = %1
set counter = 1

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

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

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%在 the 中执行,CALL并且 %count% 被 THEN-current 环境中的解析器适当地替换。

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

因此,

set count=0
set /a count = 0

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

于 2013-04-04T05:27:59.323 回答
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
于 2013-04-04T15:15:10.623 回答
0

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

于 2013-04-05T15:21:37.883 回答