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

Author Topic: Ping bat file  (Read 24122 times)

0 Members and 1 Guest are viewing this topic.

seth001

    Topic Starter


    Greenhorn

    Ping bat file
    « on: April 07, 2009, 03:25:41 AM »
    Hallo,

    I want to make a bat file that performs a ping action.
    If the result is request time out than the bat file time.bat has to be activated. The time.bat enters the time that the ping action has failed.
    EXAMPLE of ping.bat:
    :start
    ping www.google.nl -n 20 
    (After this I don’t now what to do?)
    CALL time.bat
    GOTO start


    EXAMPLE of time.bat:
    echo.|TIME >> C:\ping\Data.txt (the time will be enter in the text file Data.txt

    Can somebody help me with a function that will activate the time.bat only when there is a request time out?

    Dias de verano

    • Guest
    Re: Ping bat file
    « Reply #1 on: April 07, 2009, 05:49:04 AM »
    Code: [Select]
    :start
    ping www.whatever.com - n 20 | find "Request timed out." && (
            call time.bat
            )
    goto start

    jrebazap

    • Guest
    Re: Ping bat file
    « Reply #2 on: April 07, 2009, 06:04:24 AM »
    Hi there

    Try this code:

    @echo off
    :start
    ping www.google.nl -n 20 |findstr ms$
    if not errorlevel 1 goto start
    echo %date% %time% >> ping_log.txt
    goto start

    The last part of line 3 (|findstr ms$) is just to stop getting the whole response in the screen if you run the batch in DOS. You'll get only the very last line of the summary after the 20 pings.

    In line 5 an alternative to record date and time to a log file, only if ping fails with timeout. If ping does not fail line 4 will go to :start .

    Good luck

    seth001

      Topic Starter


      Greenhorn

      Re: Ping bat file
      « Reply #3 on: April 07, 2009, 07:07:20 AM »
      The error I get is, when the bat file starts I only see ping
      www.whatever.com - n 20 | find "Request timed out." && (call time.bat). That line get repeated  the whole time? So in steed that the ping action is activated only the line is repeated  ? ???

      Dias de verano

      • Guest
      Re: Ping bat file
      « Reply #4 on: April 07, 2009, 08:05:20 AM »
      The error I get is, when the bat file starts I only see ping
      www.whatever.com - n 20 | find "Request timed out." && (call time.bat). That line get repeated  the whole time? So in steed that the ping action is activated only the line is repeated  ? ???


      Please post the code you are using.

      seth001

        Topic Starter


        Greenhorn

        Re: Ping bat file
        « Reply #5 on: April 07, 2009, 08:25:19 AM »
        I have created a bat file named test.
        Test.bat contains :
        Code: [Select]
        :start
        ping www.whatever.com - n 20 | find "Request timed out." && (call time.bat)
        goto start

        Time.bat contains :
        Code: [Select]
        echo %date% %time% >> c:\ping\time.txt

        But the problem is that test.bat doesn’t go further than the line ping www.whatever.com - n 20 | find "Request timed out." && (call time.bat), so the ping function never gets activated.
        When I do the same line ( ping www.whatever.com - n 20 | find "Request timed out." && (call time.bat) ) in RUN, CMD the ping session starts and writes the date and time of the request time out message.

        Dias de verano

        • Guest
        Re: Ping bat file
        « Reply #6 on: April 07, 2009, 08:37:57 AM »
        Code: [Select]
        :start
        ping www.whatever.com - n 20 | find "Request timed out." && (call time.bat)
        goto start

        I just tried this code in a batch using www.bbc.co.uk (which does not service ping requests) and it tried to ping 20 times then called time.bat then started all over again. It's doing it right now.

        [update]

        10 minutes later, it's still doing it.

        I actually intended www.whatever.com to be a placeholder for whatever host you really wanted to use, but I just discovered it actually exists and also does not service ping requests.


        « Last Edit: April 07, 2009, 08:51:34 AM by Dias de verano »

        Dias de verano

        • Guest
        Re: Ping bat file
        « Reply #7 on: April 07, 2009, 08:53:02 AM »
        Does your silence mean you found your mistake?

        seth001

          Topic Starter


          Greenhorn

          Re: Ping bat file
          « Reply #8 on: April 07, 2009, 09:17:18 AM »
          Sorry for not responding right away, no I didn’t find the problem I was away from my desk. But seeing it worked for you, why can agree on the code works and that I mistyped something. I will look for the problem tomorrow and post it if I find it. Thank your very much for your time and help!

          Dias de verano

          • Guest
          Re: Ping bat file
          « Reply #9 on: April 07, 2009, 09:23:01 AM »
          Alternative code. Change the 1 to however many attempts you want to try each time.

          Code: [Select]
          @echo off
          :loop
          ping www.whatever.com -n 1
          if %errorlevel% GTR 0 call time.bat
          goto loop

          PS it is considered bad practice to name a batch file the same as a command (time).

          gh0std0g74



            Apprentice

            Thanked: 37
            Re: Ping bat file
            « Reply #10 on: April 07, 2009, 09:24:30 AM »
            Does your silence mean you found your mistake?

            you sound like those guys that post a question and couldn't wait for an answer to return and keep asking who's gonna help him.

            seth001

              Topic Starter


              Greenhorn

              Re: Ping bat file
              « Reply #11 on: April 08, 2009, 07:08:59 AM »
              I activated the bat file again today still no success. After that I created a new folder and put both bat file there (the last folder there were 9 different bat files). I run it again this time with success!

              Dias de verano

              • Guest
              Re: Ping bat file
              « Reply #12 on: April 08, 2009, 07:20:32 AM »
              the last folder there were 9 different bat files

              Was one of them called ping.bat?

              seth001

                Topic Starter


                Greenhorn

                Re: Ping bat file
                « Reply #13 on: April 08, 2009, 07:32:14 AM »
                No, but every time I changed the content of the bat file I didn’t see the change (example I changed the Ip address in 127.0.0.1 but it still goes to www.whatever.com)
                Than I created a new folder and moved the bat file there. After the changes of folders, the bat file works.

                Dias de verano

                • Guest
                Re: Ping bat file
                « Reply #14 on: April 08, 2009, 07:35:26 AM »
                maybe you were saving your changed .bat to a different folder.