You beat me to it, Sidewinder! I couldn't resist polishing my attempt
1. Informative version
@echo off
REM test success mode
set server=www.google.ca
REM test fail mode
REM set server=www.xqyrrrabc.com
set limit=5
for /L %%T in (1,1,%limit%) do (
Echo Attempting to ping %server%
REM using && (success) operator
REM opposite operator is || (failure)
ping %server% && goto ALIVE
echo Ping attempt %%T of %limit% failed.
ECHO %date%, %Time% The Internet is not available. The backup will be delayed 5 minutes.>> C:\History\Report.txt
echo Waiting 300 seconds.
TIMEOUT 300
echo.
)
REM If you got here, the internet is NOT available, checked %limit% times.
echo Internet not available.
goto DEAD
:ALIVE
REM If you got here, the internet IS available & you can attempt the vpn
ECHO %date%, %time% The Server is available, attempting to connect to the vpn. >> C:\History\Report.txt
Echo attempting to connect to VPN and perform Xcopy.
REM Code to be executed if internet is available
goto NEXT
:DEAD
REM code to be executed if internet is NOT available
:NEXT
2. Shorter functional version
@echo off
set server=www.google.ca
set limit=5
for /L %%T in (1,1,%limit%) do (
ping %server%>nul && goto ALIVE
ECHO %date%, %Time% The Internet is not available. The backup will be delayed 5 minutes. >> C:\History\Report.txt
TIMEOUT 300
)
goto DEAD
:ALIVE
REM Code to be executed if internet is available
ECHO %date%, %time% The Server is available, attempting to connect to the vpn. >> C:\History\Report.txt
goto NEXT
:DEAD
REM Code to be executed if internet is NOT available
:NEXT
More and more posters are using this technique for errorlevels:
Code:
if "%errorlevel%" NEQ "0"
Is this documented anywhere? If so where.
I'm not sure where this is documented by Microsoft; there are any number of places that discuss NT system variables.
In "regular MS-DOS" 16-bit command language, as found in MS-DOS/Win95/98/ME, you were able to test only thus: if errorlevel N (do something). No percent signs, so that errorlevel was not an ordinary variable, and N could be any nonzero integer from 1 to 255 (failure) or 0 (success). Note that "if errorlevel 1 " meant "if errorlevel is 1
or more". This method of testing errorlevel is continued in 32 bit command language.
If you wanted to find out what a nonzero value was, you had to test all values from 255 downwards thus.
if errorlevel 255 set eval=255
if errorlevel 254 set eval=254
[...]
if errorlevel 1 set eval=1
Or if you knew the topmost possible errorlevel for the program which just exited, you could start there. Tedious, I think you will agree.
However, in 32 bit command language, i.e. since Windows NT4, the "width" of errorlevel increased from 1 byte (0 to 255) to 2 bytes (0 to 65535) which makes the above method even more tedious (!) to code, and I suspect that is why we saw the introduction of the system variable %errorlevel%, the error code of the most recently used command, which allows the type of test you have noticed, where you can test for any value in one line of code.
I'm guessing that TIMEOUT is some sort of timer utility.
Seems to mimic the SLEEP command contained in the Windows 2003 Resource Kit.