0

I'm trying to put some XML code into a variable in a batch script so that I can create a file containing the XML. I also want to change one part of the XML dynamically when the script is run.

Here's how I think it should work

set MYUSERNAME=%USERNAME%
set string=
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<Extensibility xmlns="http://schemas.microsoft.com/AutomationExtensibility">
    <HostApplication>
        <Name>Microsoft SQL Server Management Studio</Name>
        <Version>*</Version>
    </HostApplication>
    <Addin>
        <FriendlyName>USAddin</FriendlyName>
        <Description>Union Square Addin for SQL Management Studio</Description>
        <Assembly>C:\Users\MYUSERNAME\Documents\Visual Studio 2010\Projects\USAddin\USAddin\bin\Debug\USAddin.dll</Assembly>
        <FullClassName>USAddin.Connect</FullClassName>
        <LoadBehavior>1</LoadBehavior>
        <CommandPreload>0</CommandPreload>
        <CommandLineSafe>0</CommandLineSafe>
    </Addin>
</Extensibility>

echo string > "somefile.txt"
4
  • 1
    Show the code you have written so far, tell us what works and where you are stuck. Commented Oct 20, 2015 at 15:46
  • The farthest I've gotten is putting a simple one line string into a variable and outputting it to a file. I got stuck on the XML because it contains quotemarks and such. Commented Oct 20, 2015 at 15:48
  • I suupose MYUSERNAME in the XML should be replaced with the MYUSERNAME given in the batch file? Commented Oct 20, 2015 at 16:05
  • Yeah, sorry I didn't make it very clear Commented Oct 20, 2015 at 16:08

1 Answer 1

1

Use findstr to extract the XML block:

@echo off
(
    for /f "delims=" %%a in ('
        findstr /r /c:"^ *<.*> *$" "%~dpnx0"
    ') do (
        setlocal enableDelayedExpansion
        set "line=%%a"
        echo !line:MYUSERNAME=%USERNAME%!
        endlocal
    )
)>"somefile.txt"

goto xmlEND

:::::::::: XML BLOCK, EACH LINE MUST START WITH < AND END WITH >

<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<Extensibility xmlns="http://schemas.microsoft.com/AutomationExtensibility">
    <HostApplication>
        <Name>Microsoft SQL Server Management Studio</Name>
        <Version>*</Version>
    </HostApplication>
    <Addin>
        <FriendlyName>USAddin</FriendlyName>
        <Description>Union Square Addin for SQL Management Studio</Description>
        <Assembly>C:\Users\MYUSERNAME\Documents\Visual Studio 2010\Projects\USAddin\USAddin\bin\Debug\USAddin.dll</Assembly>
        <FullClassName>USAddin.Connect</FullClassName>
        <LoadBehavior>1</LoadBehavior>
        <CommandPreload>0</CommandPreload>
        <CommandLineSafe>0</CommandLineSafe>
    </Addin>
</Extensibility>

:xmlEND
pause
Sign up to request clarification or add additional context in comments.

1 Comment

That will work as long as there is not any other batch code with input and output redirection on the same line. I have few batch files that do that. You may want to say it begins with < and ends with >

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.