FOR /f, parses but includes unwanted characters.


I'm recent member to this board and batch file programming in general---hello!

For several days I tried the following script with mixed success:


@Echo OFF

FOR /f "tokens=3 delims=^:^(" %%G In ('PING "hostname.com"') DO (Echo %%G)


The purpose of the script is to ping a host and parse out the number (N) in (N% loss), from the Ping summary. The script does that, but instead of a number, I get "N% loss),", and not just the integer. I'm curious why this happens and how to make the script output only the integers of the percentage and no other characters. This batch file is running under Win7


The parens are causing problems for you. But before we get to that... what you have just happens to be working as much as it is because there are not lines following the one you are interested in that have a ":" in them. I would discriminate by using FIND so that we only examine lines with the word "loss" (case insensitive). The "usebackq" is necessary because we are using quotes in the command. The "SET Scratch" lines below get rid of the parens.

@Echo OFF FOR /f "usebackq tokens=3* delims==" %%G In (`PING hostname.com ^| FIND /I "loss"`) DO SET Scratch=%%H SET Scratch=%Scratch:(=% SET Scratch=%Scratch:)=% FOR /f "tokens=2" %%G In ('echo.%Scratch%') DO SET Percent=%%G ECHO.%Percent:~0,-1%


FOR /f "tokens=3 delims=:(%%" %%G In ('PING "hostname.com"') DO (Echo %%G)

There's no need to escape virtually any characters in a delims specifier, EXCEPT for % which is escaped by doubling - caret (^) doesn't work.

Oh - and why?

If the line is parsed including ONLY ( and : as delimiters, then the line

Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),

has tokens

<ol><li>Packets </li> <li>Sent = 4, Received = 4, Lost = 0</li> <li>0% loss),</li> </ol>


  • Invoke anonymous methods
  • Implementing module pattern in Javascript with dependency on jquery
  • How do you communicate to a Modbus device within an iPhone application?
  • How can I stop sql query execution in php?
  • When using tnsping, can I specify a port number along with the SID?
  • grep: matching on literal “+”
  • Using sed to extract string values
  • How do I revert sys.stdout.close()?
  • how to insert a new column in a dataset with values if it satisfies a statement
  • TCP Ping in Silverlight
  • ELMAH: Can you set it up to email errors only remotely?
  • SIP Makefile fail (gnuwin and mingw)
  • How to override List.Add method?
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • R h2o.glm - issue with max_active_predictors
  • How to resolve docker host names (/etc/hosts) in containers
  • How to add learning rate to summaries?
  • What is the correct way to synchronize a shared, static object in Java?
  • How to disable all widgets inside Panel or inside Composite?
  • Z3: Convert between FP and BitVector?
  • Date Conversion from yyyy-mm-dd to dd-mm-yyyy
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • Initializer list vs. initialization method
  • Is there a javascript serializer for JSON.Net?
  • Java Scanner input dilemma. Automatically inputs without allowing user to type
  • Master page gives error
  • Is calc() supported in html email?
  • Counter field in MS Access, how to generate?
  • Deserializing XML into class C#
  • Where to put my custom functions in Wordpress?
  • Convert array of 8 bytes to signed long in C++
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Buffer size for converting unsigned long to string
  • need help with bizarre java.net.HttpURLConnection behavior
  • python draw pie shapes with colour filled
  • Can't mass-assign protected attributes when import data from csv file
  • sending mail using smtp is too slow
  • Binding checkboxes to object values in AngularJs
  • How to Embed XSL into XML