Batch File for Xml modification

2020-04-17 03:52发布

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.

标签: batch-file
3条回答
Deceive 欺骗
2楼-- · 2020-04-17 04:02
    @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..

查看更多
Ridiculous、
3楼-- · 2020-04-17 04:22

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
查看更多
够拽才男人
4楼-- · 2020-04-17 04:26
@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

查看更多
登录 后发表回答