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:
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?
This question is related to
batch-file
ping
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... ;)
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
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
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
For bash (OSX) ping google.com -c 1
(incase search brought you here)
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:
- Do not name your script "ping.bat".
- You can name the script as "ping.bat" but inside the script use "ping.exe google.com" instead of "ping google.com".
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
with file name abc.bat
i used Mofi sample, and change some parameters, no you can do -t
@%SystemRoot%\system32\ping.exe -n -1 4.2.2.4
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!
Source: Stackoverflow.com