Solved

Batch file with ping

Posted on 2004-04-16
7
1,357 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
  • 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 100 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

This article will show, step by step, how to integrate R code into a R Sweave document
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

820 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