Question

i am new to programming. Can some1 kindly teach me how to insert a new line in an XML file using batch script? Current file has first 2 lines:

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:xxxxxxxxx

I want to add a format string on Line 2 so it has:

<?xml version="1.0"?>
<?mso-application progid="Excel.Sheet"?>
<Workbook xmlns="urn:schemas-microsoft-com:xxxxxxxxxx

It has to be using batch-file as the rest of the file is built with it.

Cheers, Alan

Was it helpful?

Solution

@echo off
    setlocal enableextensions disabledelayedexpansion
    set "secondLine=^<?mso-application progid="Excel.Sheet"?^>"
    (for /f "delims=" %%a in (input.xml) do (
        echo(%%a
        if defined secondLine ( 
            echo(%secondLine%
            set "secondLine="
        )
    )) > output.xml

OTHER TIPS

I assume you are on Windows. If you do not already have Unix Utils, I would recommend grabbing that first and adding it to your PATH.

You can then do

 sed "1 a <?mso-application progid=\"Excel.Sheet\"?>" Input.xml > Output.xml
 move Output.xml Input.xml

Note that the Windows version of sed may not support the -i option, but if it does, you can shorten this to

sed -i "1 a <?mso-application progid=\"Excel.Sheet\"?>" Input.xml
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top