Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: batch file to loop only 3 times...  (Read 21750 times)

0 Members and 1 Guest are viewing this topic.

kalasha

    Topic Starter


    Rookie

    batch file to loop only 3 times...
    « on: July 19, 2008, 01:10:05 AM »
    i have a batch file that works for backing up but i just need to tweak one section of it.

    the batch file first checks to see if the computer can access the internet.  if not, it waits 5 minutes and tries again.  the problem is, it can do this continously.  i only want to check availability several times (3 to 5 times max,) not continously.

    >>
    :AMIALIVE
    ping www.google.ca
    if "%errorlevel%" NEQ "0" (ECHO %date%, %Time% The Internet is not available.  The backup will be delayed 5 minutes. >>C:\History\Report.txt
    TIMEOUT 300
    GOTO AMIALIVE
    ) ELSE (ECHO %date%, %time% The Server is available, attempting to connect to the vpn. >>C:\History\Report.txt
    )
    >>

    can the above be changed so that the batch file checks for connectivity only 3 times.  there are only three possible scenarios for this
    ex. first attempt gets a reply from the ping, so it continues with establishing the vpn and continues with xcopy
    or
    first attempt fails to get ping reply (do not care why) so there is a 5 minute delay before retrying.  second attempt works so (no need to retest connection a third time) it continues with establishing the vpn....
    or first, second, and third attempt fails, so it gives up and goes to EOF.

    thank you,

    jat

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: batch file to loop only 3 times...
    « Reply #1 on: July 19, 2008, 03:55:23 AM »
    More and more posters are using this technique for errorlevels:

    Code: [Select]
    if "%errorlevel%" NEQ "0"

    Is this documented anywhere? If so where.

    Anyway, this may help:

    Code: [Select]
    @echo off
    for /l %%v in (1,1,3) do (
    ping www.google.ca
    if not errorlevel 1 goto iamalive
    ECHO %date%, %Time% The Internet is not available.  The backup will be delayed 5 minutes. >>C:\History\Report.txt
    if %%v==3 goto :eof
    TIMEOUT 300
    )
    :IAMALIVE
    ECHO %date%, %time% The Server is available, attempting to connect to the vpn. >>C:\History\Report.txt

    Good luck.  8)

    I'm guessing that TIMEOUT is some sort of timer utility.
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: batch file to loop only 3 times...
    « Reply #2 on: July 19, 2008, 04:35:59 AM »
    You beat me to it, Sidewinder! I couldn't resist polishing my attempt

    1. Informative version

    Code: [Select]
    @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

    Code: [Select]
    @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

    Quote from: Sidewinder
    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.

    Quote from: Sidewinder
    I'm guessing that TIMEOUT is some sort of timer utility.

    Seems to mimic the SLEEP command contained in the  Windows 2003 Resource Kit.


    « Last Edit: July 19, 2008, 08:57:42 AM by Dias de verano »

    Sidewinder



      Guru

      Thanked: 139
    • Experience: Familiar
    • OS: Windows 10
    Re: batch file to loop only 3 times...
    « Reply #3 on: July 19, 2008, 08:37:00 AM »
    Thanks Dias. I pretty much knew what you wrote about errorlevels from my experience with a Java application which kept throwing an errorlevel 9009. Took a while to wrap my head around that one.

    At least the original method still works, unlike Microsoft playing Where's Waldo with the choice command.

     8)
    The true sign of intelligence is not knowledge but imagination.

    -- Albert Einstein

    Dias de verano

    • Guest
    Re: batch file to loop only 3 times...
    « Reply #4 on: July 19, 2008, 08:42:14 AM »
    I know people say "you can't polish a turd", but that doesn't seem to stop Microsoft trying to make their scripting language more shiny...

    kalasha

      Topic Starter


      Rookie

      Re: batch file to loop only 3 times...
      « Reply #5 on: July 19, 2008, 10:17:07 AM »
      TIMEOUT ### is a Vista dos command (my O/S).  It is the same as the Sleep utility that i downloaded from computerhope.com for the xp towers.

      SLEEP does not show how much time is left, it just shows ### seconds
      TIMEOUT ### shows a countdown from ### seconds to 0 seconds.  I use TIMEOUT on Vista to test the backup that i am writing... rename SLEEP to TIMEOUT and see if utility works...

      the code: if "%errorlevel%" NEQ "0" was from another site and the original post user wanted to do the same thing that i wanted to do - check server availability.  It was not explained on that site how the small line works, except that if there were no errors it would continue as instructed, but you need to know the code for no errors - 0 in most cases for me because i do not have any other utilities or software.

      thanks

      jat

      kalasha

        Topic Starter


        Rookie

        Re: batch file to loop only 3 times...
        « Reply #6 on: July 19, 2008, 11:18:31 AM »
        I used the code as follows:

        ECHO %date%, %time% Initiating back up...>>C:\History\Report.txt
        :AMIALIVE
           set server=www.google.ca
           set limit=5
           for /L %%T in (1,1,%limit%) do (
              ping %server%>nul && goto VPN
              ECHO %date%, %time% The Internet is not available.  The backup will be delayed 5 minutes. >> C:\History\Report.txt
              TIMEOUT 300
           )
           goto EOF

        the Report.txt file does not update the time... it looks like this:

        19/07/2008, 11:08:33.87 Initiating back up...
        19/07/2008, 11:08:33.88 The Internet is not available.  The backup will be delayed 5 minutes.
        19/07/2008, 11:08:33.88 The Internet is not available.  The backup will be delayed 5 minutes.
        19/07/2008, 11:08:56.23 The Server is available, attempting to connect to the vpn.

        should the time not update, or how do i get it to update automatically?

        jat

        Dias de verano

        • Guest
        Re: batch file to loop only 3 times...
        « Reply #7 on: July 19, 2008, 11:52:10 AM »
        The TIMEOUT syntax is

        timeout /t [seconds]

        so I suspect you need to edit that.