1

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

2 Answers 2

1
@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
Sign up to request clarification or add additional context in comments.

1 Comment

so how do i make this work for other line numbers? what if i want to insert into the 12th or the 23rd line how would I accomplish that?
0

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

1 Comment

thx Merlin, yes, I am using Window 7 on company PC, but it seems it automatically delete the files after I downloaded and extracted them.

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.