1

I have a XML file and I need to replace a perticular line say 7th line with the my own line using DOS command. How to do this?

Kjuly
  • 34,476
  • 22
  • 104
  • 118
Raveendra M Pai
  • 445
  • 2
  • 10
  • 27
  • 2
    XML files don't have "lines", and there are no "DOS commands" for working with XML. Also, this isn't a "write me some code" site. We do expect you to put some effort into doing it yourself first before posting – Ken White Oct 03 '12 at 19:39
  • I would pick a different scripting language, you could probably do it with a batch file, but it would be a proper pain. something like vbs or even a very simple c# console app would make this much simpler – SmithMart Oct 04 '12 at 07:14
  • check this http://xmlstar.sourceforge.net/ - if you have a problems with the tool just ask . Or better try to use powershell. – npocmaka Oct 04 '12 at 12:59
  • You might have some luck using vbscript. See this question: http://stackoverflow.com/questions/1115508/batch-find-and-edit-lines-in-txt-file – Br.Bill Oct 04 '12 at 21:38

1 Answers1

0

My proposal is based on the fact you identify the line by its number and not by its content

@echo OFF
REM Say LINE 7 to be removed
SETLOCAL ENABLEDELAYEDEXPANSION
SET LOWER=6
SET UPPER=8
SET INDEX=0
SET MYLINE="this is my new line"
SET OUTFILE=%~n1.new%~x1
>%OUTFILE% (
    for /F "tokens=*" %%L in (%1) do (SET /A INDEX += 1
        IF !INDEX! LEQ %LOWER% (echo %%L) ELSE break
    )
    echo %MYLINE%
    more +%UPPER% %1
)
  • echo off needed since we do a massive redirect
  • ENABLEDELAYEDEXPANSION needed for the counter in the loop
  • You just pass the xml file as an argument to the script.

HTH

Mat M
  • 1,786
  • 24
  • 30