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.bat
leads 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.
If you want the timestamp in the file too, you'd need to put:
time /T >> filename.txt
In the first example you quoted:
@ECHO OFF
:LOOPSTART
time /T >> filename.txt
ping xxx.xxx.xxx.xxx -n 4 >> filename.txt
ping yyy.yyy.yyy.yyy -n 4 >> filename.txt
sleep -m 3000
GOTO LOOPSTART
You can play around with the following code to give you a more configurable timestamp:
set time_hh=%time:~0,2%
if %time_hh% lss 10 (set time_hh=0%time:~1,1%)
set time_mn=%time:~3,2%
set time_ss=%time:~6,2%
set time_ms=%time:~9,2%
echo %time_hh%:%time_mn%:%time_ss%.%time_ms% >> filename.txt
Put all of this in place of the time /t... line
Best Answer
redirect the output to
nul
:this will still correctly set the
%errorlevel%
if the string was or was not found, but also hide the output.