16

I want to learn how to write batch scripts and tried to create a script which automatically runs this command in the command line once:

ping www.google.de -t

and displays the ping, so it would look like this:

Reply from XXX.XXX.X.XX: time=30ms
Reply from XXX.XXX.X.XX: time=31ms
Reply from XXX.XXX.X.XX: time=29ms

My problem is, that this will result in this when I execute this command as script:

Screenshot showing output on running the batch file.

My problem is that it will not execute the ping command at all, but just insert the command unlimited times in the console window as its shown in the screenshot.

I just created a new file, wrote ping www.google.de -t in it, saved it as ping.bat file and executed it with double clicking on it.

So how to write the batch file to start this command only once and display the ping result?

Mofi
  • 46,139
  • 17
  • 80
  • 143
Peter
  • 1,679
  • 2
  • 31
  • 60
  • what do you mean by when I enter this command as a script? If you put it in a batch file, you might consider an @ sign in front of it to not display the command executing. – kenny Aug 03 '14 at 17:51
  • Why is your ping running so many times? Can you post your script? – DinoOcch Aug 03 '14 at 17:54
  • Please show your code. – konsolebox Aug 03 '14 at 18:09
  • **DO NOT** name your script `ping.bat` (or any other name that is already used as a command). All it does is repeatedly calling itself. – Stephan Jul 15 '21 at 07:50

14 Answers14

50

I am sure you must have named the resultant bat file as "ping.bat". If you rename your file to something else say pingXXX.bat. It will definitely work. Try it out.

my batch file contains below code only

ping 172.31.29.1 -t

with file name as ping.bat enter image description here

with file name abc.bat

enter image description here

Dhirendra Khanka
  • 759
  • 1
  • 8
  • 21
  • Wow! Any explanation for that? It should be some reserved word, but why that behavior... – Roman Kotenko Sep 13 '16 at 15:25
  • 4
    by default, your btach program will check the current directory for any instruction. So if you call ping while a program (your bat file) has this name, then it execute that one instead of the system one. – Simon PA Dec 04 '16 at 17:50
20

Enter in a command prompt window ping /? and read the short help output after pressing RETURN. Or take a look on:

  • ping - latest Microsoft documentation for this Windows command
  • ping - Windows XP documentation for this Windows command

Explanation for option -t given by Microsoft:

Specifies ping continue sending echo Request messages to the destination until interrupted. To interrupt and display statistics, press CTRL+ENTER. To interrupt and quit this command, press CTRL+C.

You may want to use:

@%SystemRoot%\system32\ping.exe -n 1 www.google.de

Or to check first if a server is available:

@echo off
set MyServer=Server.MyDomain.de
%SystemRoot%\system32\ping.exe -n 1 %MyServer% >nul
if errorlevel 1 goto NoServer

echo %MyServer% is available.
rem Insert commands here, for example one or more net use to connect network drives.
goto :EOF

:NoServer
echo %MyServer% is not available yet.
pause
goto :EOF
Mofi
  • 46,139
  • 17
  • 80
  • 143
6

For bash (OSX) ping google.com -c 1 (incase search brought you here)

Ashley Coolman
  • 11,095
  • 5
  • 59
  • 81
4

if you want to use the name "ping.bat", a small trick is to use this code:

@echo off
cd\ 
ping google.com -t

Just add that "cd\" and you are fine... ;)

Phantom
  • 39
  • 1
  • 3
3

Not sure exactly what you are trying but your posted code should work just fine. in case you don't want the command to be displayed, add @echo off at starting of your script. If i have the below code in a file named as test.bat and run it command prompt as test.bat it will work just fine.

@echo off
ping www.google.de -t

To address your EDIT: where the main concern is ping command was not recognizable. ping command generally will be located under C:\Windows\System32\ where C:\ being the root directory. In case, the root directory is different you can get the root directory using %SystemRoot% environment variable and can say like

%SystemRoot%\Windows\System32\PING.EXE www.google.de -t

Another way to see if the command you are trying to run is recognizable or not is using WHERE command like below

where ping

If the command is recognizable; it will output the path like

C:\Windows\System32\PING.EXE

Else will result in error

Rahul
  • 76,197
  • 13
  • 71
  • 125
  • I just did what you posted and it didn't work. It just opens a blanc command window and nothing more. In my opinion it has nothing to do with the code but with my cmd.exe, path variables or something else. – Peter Aug 03 '14 at 18:16
  • How you are running it? either way, `cd` to the folder where you have saved the `bat` file and then either run from command prompt or directly by double clicking it. It works just fine. – Rahul Aug 03 '14 at 18:18
3

I know why, you are using the file name "ping" and you are using the code "ping", it just keeps trying to run itself because its selected directory in where that file is, if you want it to actually ping, put this before the ping command: "cd C:\Windows\system32", the actual file that pings the server is in there!

1

From Batch file, ping a ip only once using the following command:
Ping 192.168.199.10 -n 1

enter image description here

A. Gopal Reddy
  • 370
  • 1
  • 3
  • 16
  • how does this add useful information to the accepted (four years old) answer? – Stephan Jun 12 '18 at 09:35
  • This answers the question from a google search perspective. I needed to look this up while on my iPhone to tell someone. Very useful - thanks for the answer. – Sql Surfer Oct 05 '18 at 09:20
0

i used Mofi sample, and change some parameters, no you can do -t

@%SystemRoot%\system32\ping.exe -n -1 4.2.2.4

Hassan Faghihi
  • 1,888
  • 1
  • 37
  • 55
0

The only thing you need to think about in this case is, in which directory you are on your computer. Your command line window shows C:\users\rei0d\desktop\ as your current directory.

So the only thing you really need to do is:
Remove the desktop by "going up" with the command cd ...

So the complete command would be:

cd ..
ping XXX.XXX.XXX.XXX -t
jAC
  • 5,195
  • 6
  • 40
  • 55
Tor
  • 1
  • No, you don't have to do anything. Since `C:\Windows\System32` is written into the `PATH` variable, `ping` works everywhere (globally). If it's not working for you, then you have to check your `PATH` variable. `echo %PATH%` should return the path mentioned. If not, edit it via `Control Center -> System -> Advanced System settings -> Advanced Tab -> Path variables`. – jAC Oct 25 '16 at 09:21
0

Having 2 scripts called test.bat and ping.bat in same folder:

Script test.bat contains one line:

ping google.com

Script ping.bat contains below lines:

@echo off
echo Hello!
pause

Executing "test.bat" the result on CMD will be:

Hello!
Press any key to continue . . .

Why? Because "test.bat" is calling the "ping.bat" ("ping google.com" is interpreted as calling the "ping.bat" script). Same is happening if script "ping.bat" contains "ping google.com". The script will execute himself in a loop.

Easy ways to avoid this:

  1. Do not name your script "ping.bat".
  2. You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
Community
  • 1
  • 1
DragosD
  • 1
  • 1
0

Create a text file with text "@%SystemRoot%\system32\ping.exe -t www.google.com" and save it with extension ".bat". Just click and run it and you will get the result.

Result

So basically what happens is that we run ping.exe application with parameters '-t' and 'www.google.com' (web-address).

machineman
  • 31
  • 5
0

The answer to your question is this

Ping -n 1 0.0.0.0

But if you want it to be faster than this, this will be your answer

Ping -n 1 -l 1 0.0.0.0

Note: Replace 0.0.0.0 with your desired IP address

0

Create a "ping google.bat" file with the following text:

PING.EXE -t -l 666 google.com

"-t" means continuous

"-l 666" means bigger packets (666 bytes each)

AMC
  • 1
  • 1
  • You should mention why this works when the OPs original code does not (it calls ping.exe directly and not the OPs ping.bat recursively). Also, please use back-ticks to format text as code. – thebjorn Jul 23 '23 at 01:05
-1

Just write the command "ping your server IP" without the double quote. save file name as filename.bat and then run the batch file as administrator