Help needed writing batch script!


  1. Posts : 32
    Windows 11 Pro x64
       #1

    Help needed writing batch script!


    Firstly thank you for even clicking on this thread as I understand that this probably isn't meant to be here.


    The scenario is that I'm trying to write a script that incorporates the result of a ping to an IP and then based on the result do different things.

    Here is an example of what I'm trying to achieve:

    --

    ping IP

    if ping successful, do this

    otherwise

    do this..

    --

    I'm a complete novice at script writing so any help would be appreciated.

    Thanks.
      My Computer


  2. Posts : 8,103
    windows 10
       #2

    What are you trying to do as there may well be freeware to do it why reinvent the wheel. Depending what you want to do there is a free script program that can do all these things with very simple cmds https://www.autoitscript.com/site/autoit/
      My Computer


  3. Posts : 16,932
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    You could do something like this but there are many variations on this theme so explaining just what your objective is remains the best policy -

    This code inspects the ping response for "TTL", a term that you will see only in successful ping responses, and then goes to the Connected label if it is successful.
    The line beginning ::::: is an alternative that takes the opposite view - it goes to Disconnected if the ping fails. I have written the whole lot so you can choose whichever of those lines you want without having to change any of the rest. Just move the ::::: from one of these lines to the other to compare their performance.
    You would enter the IP address you want where I have written 8.8.8.8 and write your own actions where I have written, "pause during ..."


    Code:
    ping -n 1 8.8.8.8 | findstr TTL && goto Connected
    ::::: ping -n 1 8.8.8.8 | findstr TTL || goto Disconnected
    :Disconnected
    pause during Disconnected
    GoTo EndWWWCheck
    :Connected
    pause during Connected
    GoTo EndWWWCheck
    :EndWWWCheck
    pause at EndWWWCheck during testing

    Denis
      My Computer


  4. Posts : 10,740
    Windows 11 Workstation x64
       #4

    This may be better achieved in PowerShell using something like the test connection command, I'm sure some of the folks that know about PowerShell may be able to expand on that.
      My Computers


  5. Posts : 16,932
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #5

    Britton,

    - If you explain just what you are trying to achieve, people will be able to provide better assistance.
    - Just by way of example, I would not use the script I posted for you because it does not suit my circumstances. One of the networks I use blocks all "ICMP" traffic, which includes both pinging & the PowerShell script linked to above, so I have had to adopt a different solution.
    - You can check your version of PowerShell by entering this command in a PowerShell window [the answer will be version 5.1] $PSversionTable
    -
    The PS version 5.1 script guidance for the test connection command linked to above is https://docs.microsoft.com/en-us/pow...powershell-5.1

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 17:33.
Find Us




Windows 10 Forums