Why does the ping command in my batch file execute in a loop?

31,002

Solution 1

It is a bit unclear what is exactly the problem you face since you don't provide any output or screenshot of what you don't like, but I'll explain the two most likely problems I see:

Given your script is called ping.bat and looks like this:

 ping example.com

then the interpreter (cmd.exe) searches/probes the paths in the environment variable %PATH% for something that looks like ping ... and it does that by appending each suffix from %PATHEXT% which contains something like .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC. so, calling just ping from the ping.batleads to a search for ping.com ping.exe ping.bat and so on. The interpreter will find a ping.bat in the current working directory (your ping.bat) and calls it.

So, you will have a nice recursion here: ping.cmd executes the first line, searches for "ping", finds "ping.cmd", executes the first line, searches for "ping", finds "ping.cmd", executes the first line, searches for "ping", finds "ping.cmd" ...

The second problem you might have is this:

The interpreter of the batch file will usually repeat the commands you have written to the .bat/.cmd file. Thus something like this ping www.superuser.com will look like this:

 C:\Users\XYZ\Desktop>ping www.superuser.com

 Ping wird ausgeführt für superuser.com [64.34.119.12] mit 32 Bytes Daten:
 Antwort von 64.34.119.12: Bytes=32 Zeit=110ms TTL=46
 Antwort von 64.34.119.12: Bytes=32 Zeit=107ms TTL=46

If you want to get rid of C:\Users\XYZ\Desktop>ping www.superuser.com in the output of the script then you have to either prepend each line with an @ (for example, '@ping www.superuser.com') in the script or place a @echo off before the bunch of command lines you want to execute "quietly".

TL;DR; Don't call your bat files the same as existing programs.

Solution 2

I had the same problem,

ping.bat contained several lines of ping command

ping host1
ping host2
ping host3
...

output in cmd shell looked as below and continued in a loop on and on until the break with ctrl+C

c:\ping host1    
c:\ping host1    
c:\ping host1    
c:\ping host1    
c:\ping host1
...

c:\ping host1
^CTerminate batch job (Y/N)?

To solve it, just rename the ping.bat, so it doesn't call itself in the batch file or use ping.exe as the command in the batch file

ping.exe host1
ping.exe host2
ping.exe host3
...

Solution 3

I had the same issues. Here is the resolution:

Rename your ping.bat file to pingtest.bat

And ensure it contains:

ping xxx.xxx.xxx.001
ping xxx.xxx.xxx.002
ping xxx.xxx.xxx.003
pause

This will allow you to ping one or multiple addresses and will show the results on screen. This batch file can be run from anywhere on your PC by double clicking on it.

Solution 4

The -t parameter makes the ping continuous.

Maybe remove the -t parameter and then tell your batch script to wait for you to press enter before killing the cmd screen.

Haven't used windows in a while but this should work:

ping xxx.xxx.xxx.xxx
pause
exit

Solution 5

Right click - Run As Administrator worked for me.

Share:
31,002

Related videos on Youtube

pradeetp
Author by

pradeetp

Updated on September 18, 2022

Comments

  • pradeetp
    pradeetp over 1 year

    I created a .BAT file in Windows 7 that has the following line:

    PING XXX.XXX.XXX.XXX -t
    

    (XXX replaces the actual IP number). However, when I double click on this batch file, I can see the ping command repeatedly being executed in a loop. I even tried to rename the ping.BAT to ping.CMD but the result is the same.

    I want to avoid writing ping command through the command prompt, which is why I created the batch file. I don't know why the ping command is being continuously called when the same statement is put in a batch file.

  • pradeetp
    pradeetp almost 13 years
    removing -t didn't work. Could you please post the script?
  • n0pe
    n0pe almost 13 years
    @pradeetp added some code above
  • nixda
    nixda over 10 years
    I could swear that this isn't correct and tested it - and he is right O_O
  • nixda
    nixda about 10 years
    That answer is the same as stefan and akira already gave. Only shorter