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

Author Topic: [Batch] Ping Script?  (Read 16680 times)

0 Members and 1 Guest are viewing this topic.

CaTaLinU

    Topic Starter


    Rookie

    • Experience: Beginner
    • OS: Unknown
    [Batch] Ping Script?
    « on: May 09, 2012, 08:36:02 AM »
    It is possible to make something like this ...
    Code: [Select]
    @ECHO OFF
    Title Service Check
    CD\
    CLS

    Rem Step 1:
    Ping 95.76.53.219
    echo Your connection to the 95.76.53.219 is %ms%
    Pause


    I want to show only the average in Ms with that echo ?...

    it is possible ?

    Squashman



      Specialist
    • Thanked: 134
    • Experience: Experienced
    • OS: Other
    Re: [Batch] Ping Script?
    « Reply #1 on: May 09, 2012, 09:39:06 AM »
    ping_avg.bat
    Code: [Select]
    @echo off
    setlocal enabledelayedexpansion
    set count=0
    set total=0
    for /F "tokens=3,7 delims==: " %%G in ('ping yahoo.com ^| findstr /C:"Reply from"') do (
    set /a count+=1
    SET ms=%%H
    set ms=!ms:ms=!
    set /a total=!total! + !ms!
    )
    set /a average=%total% / %count%
    echo Your connection to Yahoo.com is %average%ms

    CaTaLinU

      Topic Starter


      Rookie

      • Experience: Beginner
      • OS: Unknown
      Re: [Batch] Ping Script?
      « Reply #2 on: May 09, 2012, 10:11:55 AM »
      not working :((

      the console remain blank ....

      @ Windows 7 Ultimate

      Salmon Trout

      • Guest
      Re: [Batch] Ping Script?
      « Reply #3 on: May 09, 2012, 10:34:52 AM »
      not working :((

      Fine here...

      Code: [Select]
      C:\Batch\Test>aveping.bat
      Your connection to Yahoo.com is 188ms

      C:\Batch\Test>


      Windows 7 Professional

      Squashman



        Specialist
      • Thanked: 134
      • Experience: Experienced
      • OS: Other
      Re: [Batch] Ping Script?
      « Reply #4 on: May 09, 2012, 10:35:53 AM »
      not working :((

      the console remain blank ....

      @ Windows 7 Ultimate
      Don't know what to tell you.  Works just fine for me.
      Code: [Select]
      C:\batch files>ping_avg.bat
      Your connection to Yahoo.com is 49ms

      CaTaLinU

        Topic Starter


        Rookie

        • Experience: Beginner
        • OS: Unknown
        Re: [Batch] Ping Script?
        « Reply #5 on: May 09, 2012, 10:52:39 AM »
        :(( F**king windows .........

        Someoen knows from what is this "error"
        For Salmon Trout worked this script but for me the console remain blank...

        Salmon Trout

        • Guest
        Re: [Batch] Ping Script?
        « Reply #6 on: May 09, 2012, 10:53:43 AM »
        Maybe he doesn't want to do batch arithmetic; perhaps he just wants to isolate the average part of the ping output summary line...

        C:\Batch\Test>ping www.yahoo.com

        Pinging eu-fp3.wa1.b.yahoo.com [87.248.122.122] with 32 bytes of data:
        Reply from 87.248.122.122: bytes=32 time=52ms TTL=52
        Reply from 87.248.122.122: bytes=32 time=51ms TTL=52
        Reply from 87.248.122.122: bytes=32 time=75ms TTL=52
        Reply from 87.248.122.122: bytes=32 time=50ms TTL=52

        Ping statistics for 87.248.122.122:
            Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
        Approximate round trip times in milli-seconds:
            Minimum = 50ms, Maximum = 75ms, Average = 57ms


        easy enough...

        for /f "tokens=1-9" %%A in ('ping www.yahoo.com ^| find "Average ="') do echo Average=%%I

        Salmon Trout

        • Guest
        Re: [Batch] Ping Script?
        « Reply #7 on: May 09, 2012, 10:54:46 AM »
        For Salmon Trout worked this script but for me the console remain blank...

        It worked for Squashman too.

        1. Please don't use bad language, even censored.
        2. How are you running this code?


        CaTaLinU

          Topic Starter


          Rookie

          • Experience: Beginner
          • OS: Unknown
          Re: [Batch] Ping Script?
          « Reply #8 on: May 09, 2012, 11:01:27 AM »
          Bat File Name - ping.bat

          code given by Squashman is not changed, i run it how he give it to me

          the Double-Click on ping.bat
          if i wait more than 30 sec my pc it will work very slowly ..

          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: [Batch] Ping Script?
          « Reply #9 on: May 09, 2012, 11:16:46 AM »
          Maybe he doesn't want to do batch arithmetic; perhaps he just wants to isolate the average part of the ping output summary line...

          C:\Batch\Test>ping www.yahoo.com

          Pinging eu-fp3.wa1.b.yahoo.com [87.248.122.122] with 32 bytes of data:
          Reply from 87.248.122.122: bytes=32 time=52ms TTL=52
          Reply from 87.248.122.122: bytes=32 time=51ms TTL=52
          Reply from 87.248.122.122: bytes=32 time=75ms TTL=52
          Reply from 87.248.122.122: bytes=32 time=50ms TTL=52

          Ping statistics for 87.248.122.122:
              Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
          Approximate round trip times in milli-seconds:
              Minimum = 50ms, Maximum = 75ms, Average = 57ms


          easy enough...

          for /f "tokens=1-9" %%A in ('ping www.yahoo.com ^| find "Average ="') do echo Average=%%I
          That makes the code a lot easier.  Not sure why I didn't do that in the first place.

          Salmon Trout

          • Guest
          Re: [Batch] Ping Script?
          « Reply #10 on: May 09, 2012, 11:19:04 AM »
          Bat File Name - ping.bat

          code given by Squashman is not changed, i run it how he give it to me

          the Double-Click on ping.bat
          if i wait more than 30 sec my pc it will work very slowly ..

          Sounds about right, mine took nearly 23 seconds. Maybe you have a slow connection?


          C:\Batch\Test>(
          More? echo %time%
          More? aveping.bat
          More? echo %time%
          More? )
          18:18:24.61
          Your connection to Yahoo.com is 158ms
          18:18:47.23


          Squashman



            Specialist
          • Thanked: 134
          • Experience: Experienced
          • OS: Other
          Re: [Batch] Ping Script?
          « Reply #11 on: May 09, 2012, 11:21:17 AM »
          Bat File Name - ping.bat

          code given by Squashman is not changed, i run it how he give it to me

          the Double-Click on ping.bat
          if i wait more than 30 sec my pc it will work very slowly ..
          Well there is your mistake.  You can't name your batch file the same as a command in your batch file!!!!!!!

          Salmon Trout

          • Guest
          Re: [Batch] Ping Script?
          « Reply #12 on: May 09, 2012, 11:24:40 AM »
          Well there is your mistake.  You can't name your batch file the same as a command in your batch file!!!!!!!

          I missed that... I just assumed nobody would ever call a batch script ping.bat...

          CaTaLinU

            Topic Starter


            Rookie

            • Experience: Beginner
            • OS: Unknown
            Re: [Batch] Ping Script?
            « Reply #13 on: May 09, 2012, 11:29:27 AM »
            I missed that... I just assumed nobody would ever call a batch script ping.bat...

            Now works fine :D

            Thx a lot :D

            Squashman



              Specialist
            • Thanked: 134
            • Experience: Experienced
            • OS: Other
            Re: [Batch] Ping Script?
            « Reply #14 on: May 09, 2012, 12:17:06 PM »
            Now works fine :D

            Thx a lot :D
            Would probably run a tad faster if you used Salmon Trout's code. 

            CaTaLinU

              Topic Starter


              Rookie

              • Experience: Beginner
              • OS: Unknown
              Re: [Batch] Ping Script?
              « Reply #15 on: May 09, 2012, 12:38:09 PM »
              now i am waiting ~ 5 sec ....

              Salmon Trout

              • Guest
              Re: [Batch] Ping Script?
              « Reply #16 on: May 09, 2012, 01:08:48 PM »
              Regarding the issue of giving a script the same name as a Windows command, the situation is this: at the prompt, or in a script, when the command interpreter cmd.exe finds something which might be a request to execute something, (without an extension) in this situation it was 'ping', it first looks in the current folder for an executable file with that base name. If there are more than one matching file, it chooses the first extension listed in the PATHEXT system variable. The default value of this is .COM; .EXE; .BAT; .CMD; .VBS; .VBE; .JS ; .WSF; .WSH. If it does not find anything in the current folder, cmd.exe then does the same in turn for each folder listed in the PATH system variable, in the order that they are listed. The default value of PATH for XP is C:\Windows\System32\;C:\Windows\;C:\Windows\System32\Wbem. So if you insist on calling a script ping.bat, and having the Windows ping command in it, then use the full name of the Windows command, that is ping.exe, within the script. For absolute avoidance of name collisions, avoid the search of the PATH folders by using the full command path as well, e.g. C:\Windows\system32\ping.exe.

              Note that the above applies to commands which are separate programs. If you insisted on having a script called dir.bat, and you typed DIR at the prompt, the sript wouldn't run, because DIR is an internal Windows command, not a separate program like ping. You'd have to type dir.bat.