3

So, I want to include in one of my batch file projects a way to create an XML file. I already have a way of parseing XML files to get the data out of them, so that is of no matter. Anyways.

Let's say I have 3 variables. One called package, another package-ver and lastly one called package-url.

I want them to be created into an XML file like this

<?xml version="1.0"?>
   <package-info>
      <package data="%package%">
      <package-ver data="%package-ver%">
      <package-url data="%package-url%">
   </package-info>

I would then preceed to have this be pipped into a text file. But, I don't want to have to tell it to do this every single time, how can I have it so I can give, let's say a function or program input and it'll create all of this for me.

How would I do this?

-- EDIT -- Added other two variables, sorry.

  • 1) I see only a single variable in your XML, where are the other two? 2) What do you want to do exactly? Pass three strings as command line arguments to a batch file and have it generate an XML with those strings inserted at the appropriate locations? – Karan May 31 '13 at 3:05
  • 1
    Have you googled your question already? look here – Endoro May 31 '13 at 9:21
  • @Karan, That's exactly what I'm trying to do. 3 vartiables that are then put into the associated locations in an xml file, as you stated. – Jared Allard Jun 1 '13 at 22:54
6

You can use %1, %2, %3, etc. to select the command line arguments and ^ to escape characters.

Example: call this batch file with the arguments batch.bat <package> <package-ver> <package-url> <path to output file>. It will output to the specified file (but without linebreaks, that can be done by encapsulating the echo command in parentheses combined with using && and echo.)

@echo off
echo ^<?xml version="1.0"?^>^<package-info^>^<package data="%1"^>^<package-ver data="%2"^>^<package-url data="%3"^>^</package-info^> > %4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.