Solved

Batch file with ping

Posted on 2004-04-16
7
1,355 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
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 …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

823 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