0

I am trying to write a batch to execute a script overnight for a path that won't be created till late at night. I want the loop to check a total of 12 times if the path exists. If it does exist, then I want it to start the script tests. If it doesn't exist, then I want it to sleep for an hour and check again. I also want the loop to tell me how many times it has looped again each time. Here is what I have...

@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>

Notice how it says sleep is an unrecognized command and the %Timer% is blank

Fiddle Freak
  • 1,347
  • 1
  • 23
  • 56
  • See [here](http://stackoverflow.com/questions/166044/sleeping-in-a-batch-file) for how to sleep in a batch file. You need to use `SET /A` when performing arithmetic operations, i.e. `set /A Timer = %counter% + %Timer%`. – Simon MᶜKenzie Apr 04 '13 at 05:19
  • Did you try using `timeout`? – devnull Apr 04 '13 at 05:27

3 Answers3

2
set build = %1
set counter = 1

and similar are the likely source of your problems.

The spaces ARE significant. The line set build = %1 for instance sets the variable "build" - with a space, not "build" and the value set would be "%1", not "%1" - the first parameter but WITH a leading space.

SLEEP is not a standard executable. try TIMEOUT (execute

timeout /?

from the prompt for documentation.

And TIMER is not set anywhere - it would be replaced by [nothing]


Addenda: a couple of demonstration routines

@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.


Amendment to demonstrate use of CALL to achieve the same end-result as 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%%
)

What happens here is that the parser is used to retrieve the CURRENT (ie run-time) value of COUNT. What it does is actually replace %%count%% with %count% because % escapes % and so it sees the second % as an ordeinary character, having no special meaning. Thus the line (eg) ECHO Loop 3: count=%count% is executed in the CALL and %count% is duly replaced by the parser from the THEN-current environment.

Note also the SET/A statement. SET/A is a later addtion to SET and assigns the arithmetic result of the expression to the variable - converted back to a string as ALL environment variables are strings. The syntax I've used is a C-style expression to add 1 to the target. It could equally well have been expressed as SET /A count=%count% + 1 or even set /a count = count + 1 because the first expression would be parsed to (eg) SET /a count=3 + 1 and the semantics of the SET/A are such that spaces (not being digits) are ignored throughout the evaluation of the statement.

Hence,

set count=0
set /a count = 0

both achieve the same aim, but only when the /a opion is used.

Magoo
  • 68,705
  • 7
  • 55
  • 76
0

I ended up going with something that works so far. I just haven't figured out how to add a counter within that for loop. You are saying it was because I had a space between counter = 1 right?

Here is what I have used:

@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
Fiddle Freak
  • 1,347
  • 1
  • 23
  • 56
0

For the retries (I don't know why I didn't think of this sooner), I can just add the line echo %%a out of 12 tries within the loop.

Fiddle Freak
  • 1,347
  • 1
  • 23
  • 56
  • `for /l %%a in (12,-1,1) do (` is also legal. I'll add a small amendment to my response to show how you **COULD** use `count` in your original code - but first steps first. Be confident with the variables you're using. Then on to more advanced techniques. – Magoo Apr 05 '13 at 15:28