Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch file with ping

Posted on 2004-04-16
7
Medium Priority
?
1,364 Views
Last Modified: 2012-06-27
@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success--
    goto END
  )
)

echo !!Failed!!
:END

This code in my bat file returns whether a ping was sucessful or failed. ("Svar" is the swedish version of "Response" as in "Response received from xxx.xxx.xxx.xxx")

How do I add to the above code to make it show the time it too to receive the response when the ping is succesful?

Regards,
MHQ
0
Comment
Question by:MHQ
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:mrdtn
ID: 10843348
Simple.  Use %date% and %time% as shown below.

@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success-- at %date% %time%
   goto END
 )
)

echo !!Failed!!
:END
0
 
LVL 3

Author Comment

by:MHQ
ID: 10844508
I'm sorry, I missed a "k" in my post.. though you should have been ablse to see what I ment anyhow.. I want to display the time it tooK to receive the responce.. that is, the time in ms reported by the ping. Sorry for the confusion
0
 
LVL 6

Accepted Solution

by:
mrdtn earned 400 total points
ID: 10844580
Extract token 5 as well.  It will show up as replacebale param %%t.  Let me know if this is what you were looking for.

--

mrdtn

--

@echo off
set /a errors=0
echo Trying to ping host %1
For /f "tokens=1,5" %%s in ('ping "-n" "1" %1') DO (
  if "%%s"=="Svar" (
    echo --Success-- %%t
    goto END
  )
)

echo !!Failed!!
:END
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Author Comment

by:MHQ
ID: 10844874
That worked perfectly! But... how come it is stored in %%t ? What is that based on?
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 10845788
That's just the way it works.  Open a command window and type "for /?" for a long list of information.

Basically, when you tell the for statement to get more than one token, it assigns them one by one in sequence beginning with the letter of the variable you specified in the statement, so for example:

for /f "tokens=1-5,*" %%a . . .

will assign tokens to %%a, %%b, %%c, %%d, %%e . . .

The "*" tells the statement to assign anything that might be left over to yet a 6th token, which will be %%f.

Therefore, if you have a lot to parse, best not to start with x, y, or z.

--

mrdtn
0
 
LVL 3

Author Comment

by:MHQ
ID: 10845843
Ic, didn't notice that "t" is after "s".. makes sence =) thanx alot!
0
 
LVL 6

Expert Comment

by:mrdtn
ID: 10846019
It's subtle . . . but then if it weren't, it wouldn't be so much fun!!

Cheers,

mrdtn
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
This article will show how Aten was able to supply easy management and control for Artear's video walls and wide range display configurations of their newsroom.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question