0

I have a XML file generated by Visual Studio that contains the version of the product, hence the tag will always be there just the value changes. I need to extract the version with windows command line somehow. This is the tag in the file and I need only the version number "1.0.0.0":

 <?define BuildVersion = 1.0.0.0 ?>

Is there a built in tool (maybe with findstr) that can accomplish this?

wasp256
  • 5,943
  • 12
  • 72
  • 119
  • Based on [this post](https://stackoverflow.com/a/40784439/276311) I came up with this: `for /F "tokens=4 delims= " %%E in (' findstr "BuildVersion" "file.xml" ') do @echo %%E` – Adriano P Mar 28 '18 at 12:59
  • Results in `%%E was unexpected at this time` – wasp256 Mar 28 '18 at 13:02
  • Use a single % instead of two when running from the command line. – Adriano P Mar 28 '18 at 13:04
  • Awesome it works! Can you put it as an answer please – wasp256 Mar 28 '18 at 13:05
  • @AdrianoP, the spaces around `=` are optional, so I would include that character to the delimiters like `delims== ` and use `tokens=3` instead... – aschipfl Mar 28 '18 at 13:24
  • In general it is a better idea to use a language that is capable of handling XML data natively rather than treating them like normal text... – aschipfl Mar 28 '18 at 13:25

1 Answers1

1

Use for /F loop to parse the output of findstr and extract the text you need (using space as delimiter, get the 4th token):

for /F "tokens=4 delims= " %%E in ('
    findstr "BuildVersion" "file.xml" 
') do @echo %%E

Use a single % instead of two when running from the command line though.

For a more comprehensive information please check this post.

Adriano P
  • 2,045
  • 1
  • 22
  • 32