2

i have an xml file on which i have to do modification basing on some rules and i want to know can we do that in the batch script. suppose the following is the sample of xml file

> > <conf-article article-type="research" content-type="----"
> > dtd-version="---" open-access="no" xml:lang="en" lifecycle="final">
> > <conf-front>
    <conf-proc-meta>
    <conf-proc-id
> > conf-proc-id-type="conf-acronym">cccc</conf-proc-id>
> > <conf-proc-title-group>

and i want to insert the following lines

<!--Delivery Date: 07/23/2013-->
<!--XML Script: sdfasdfdfs-->
<!--Batch: sdfsdfdssfs-->

before <conf-front> tag.This is an example like that i have some more.so i need some help on this.

2
  • Is <conf-front> always the third line? Are lines 1 and 2 always the same or are they dynamic? Commented Oct 24, 2013 at 13:24
  • It can be either 4th or 5th line always Commented Oct 24, 2013 at 13:31

3 Answers 3

1

Here is another solution to process a folder full of xml files.

It uses a helper batch file by Aacini called findrepl.bat from - https://www.dropbox.com/s/rfdldmcb6vwi9xc/findrepl.bat

Place findrepl.bat in the same folder as the batch file, and remove the REM to make the .tmp version of the files overwrite the original files, after you test it.

@echo off
for %%a in (*.xml) do (
   echo processing "%%a"
      type "%%a" |findrepl "." /e:"<conf-front>" /o:-1:-1 >"%%a.tmp"
         >>"%%a.tmp" echo ^<!--Delivery Date: 07/23/2013--^>
         >>"%%a.tmp" echo ^<!--XML Script: sdfasdfdfs--^>
         >>"%%a.tmp" echo ^<!--Batch: sdfsdfdssfs--^>
      type "%%a" |findrepl /v "." /e:"<conf-front>" /o:-1:-1 >>"%%a.tmp"
   REM move "%%a.tmp" "%%a"
)
pause
Sign up to request clarification or add additional context in comments.

Comments

1
@ECHO OFF
SETLOCAL
(
 FOR /f "delims=" %%a IN (q19559569.xml) DO (
  ECHO("%%a"|FIND "<conf-front>" >NUL
  IF NOT ERRORLEVEL 1 (
    ECHO(^<!--Delivery Date: 07/23/2013--^>
    ECHO(^<!--XML Script: sdfasdfdfs--^>
    ECHO(^<!--Batch: sdfsdfdssfs--^>
  )
  ECHO(%%a
 )
)>newfile.xml
GOTO :EOF

where q19559569.xml is your original and newfile.xml is created.

Batch is really not a suitable tool for the task.

I've assumed that by "before" you mean "on the line preceding that on which the nominated tag appears" and not "directly before the tag" - that is, between the leading > > and the tag.

Since the inserted text nominated is clearly artificial, if the real text you require is placed in a file called say insertme.txt then replacing the three ECHO statements with

type insertme.txt

should be more flexible.


@ECHO OFF
SETLOCAL
FOR %%x IN (*.xml) DO (
 FOR /f "delims=" %%a IN ('type "%%x"') DO (
  ECHO("%%a"|FIND "<conf-front>" >NUL
  IF NOT ERRORLEVEL 1 (
    ECHO(^<!--Delivery Date: 07/23/2013--^>
    ECHO(^<!--XML Script: sdfasdfdfs--^>
    ECHO(^<!--Batch: sdfsdfdssfs--^>
  )
  ECHO(%%a
 )
)>"%%~nx.new"
GOTO :EOF

This mod should produce a new file with extension .new from the existing .xml files

3 Comments

if i want the script itself to run for all xml files in the folder then ? and it should write to the same file and the remaining content should not be removed.
Really need to be much more specific. Do you want to run on all .xmls producing a new file corresponding to EACH individual .xml, or one gigantic .xml containing all of the data from your original set, each adjusted? This batch should not change anything other than injecting the three lines before the line(s) containing <conf-front> You could check using FC your.xml newfile.xml
no not one gigantic xml file. It should write in the corresponding file nonly
0
    @echo off

    ::get the line before desired tag
    for /f "tokens=1 delims=:" %%L in ('findstr /n "<conf-front>" some.xml') do set /a line=%%L-1

    ::create empty file 
    break>"%temp%\empty"

    ::get first <%line%> lines of the file 
    fc "%temp%\empty" "some.xml" /lb  %line% /t |more +4 | findstr /B /E /V "*****" >temp.xml

    :: add additional content
    echo ^<!--Delivery Date: 07/23/2013--^>  >> temp.xml
    echo ^<!--XML Script: sdfasdfdfs--^>  >> temp.xml
    echo ^<!--Batch: sdfsdfdssfs--^>  >> temp.xml

    ::get last <%line%> lines of the file
    type "some.xml" | more +%line%  >> temp.xml

    ::delete temp empty file
    del /Q /F "%temp%\empty"

This will create temp.xml file and you can check if it fits on your needs and then copy it over your old file.Will set this before last <conf-front> tag so I hope there's only one of this kind.And change some.xml with the name of your xml everywhere in the code.

EDIT ~ do this with all .xml in a folder (will not affect subfolders)

 @echo off

 rem cd /d "c:\some_dir"

::create empty file 
break>"%temp%\empty"

:: iterate trough the xml files
setlocal enableDelayedExpansion
for %%X in (*.xml) do (


    set "line="
    ::get the line before desired tag
    for /f "tokens=1 delims=:" %%L in ('findstr /n "<conf-front>" %%~dpsnxX') do set /a line=%%L-1

    ::only in case the tag is presented
    if defined line (

        ::get first <%line%> lines of the file 
        fc "%temp%\empty" "%%~dpsnxX" /lb  !line! /t |more +4 | findstr /B /E /V "*****" >%temp%\temp.%%~nX.xml

        :: add additional content
        echo ^<^^!--Delivery Date: 07/23/2013--^>  >>"%temp%\temp.%%~nX.xml"
        echo ^<^^!--XML Script: sdfasdfdfs--^>  >>"%temp%\temp.%%~nX.xml"
        echo ^<^^!--Batch: sdfsdfdssfs--^>  >>"%temp%\temp.%%~nX.xml"

        ::get last <%line%> lines of the file
        type "%%~dpsnxX" | more +!line!  >>"%temp%\temp.%%~nX.xml"

    )

)
endlocal

:: replace the file with these in temp folder with new content
for %%X in (*.xml) do (
    move /Y "%temp%\temp.%%~nX.xml" "%%~dpnxX" 2>nul
)

::delete temp empty file
del /Q /F "%temp%\empty"

Just change c:\some_dir with your own path.And back-up your dir..

12 Comments

I have 100 xml files in one folder with different names so how to run this code to change all the xml files.
Can you check it now?
Hi i have tried using your code with the changes u have told to do. But i am getting the following error as Cannot access file F:\Documents and Settings\sys\Desktop\test1\+ The process tried to write to a nonexistent pipe. FC: Invalid Switch
This will happen if you have a file without this tag.I'll edit it again :-)
Just a comment to say that FC is not a reliable tool in text mode.
|

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.