Batch Insert Element from XML with Shared Filename

后端 未结 3 1529
春和景丽
春和景丽 2020-12-12 06:31

I\'m trying to insert 800 unique this is a remark elements into an existing set of 800 XML files. I generated 800 documents

3条回答
  •  执念已碎
    2020-12-12 07:01

    This problem have an interesting aspect, so I used it to test a different method to process files.

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Process all .xml files in current directory
    for %%a in (*.xml) do (
    
       rem Locate the line numbers where "CHANGETIME" and "/ENTRIES" appears
       set "insertLine="
       for /F "delims=:" %%b in ('findstr /N "CHANGETIME /ENTRIES" "%%a"') do (
          if not defined insertLine (
             set "insertLine=%%b"
          ) else (
             set "lastLine=%%b"
          )
       )
    
       rem Block used to read-input-file/create-output-file
       < "%%a" (
    
               rem Read the first line from input file
               set /P "line="
    
               rem Copy lines up to the insertion point
               for /L %%i in (1,1,!insertLine!) do set /P "line=!line!" & echo/
    
               rem Insert the corresponding REMARK file
               type "RemarksFolder\%%a"
    
               rem Copy the rest of lines
               set /A insertLine+=1
               for /L %%i in (!insertLine!,1,!lastLine!) do set /P "line=!line!" & echo/
    
               ) > "output.tmp"
       rem Block-end
    
       rem Replace input file with created output file
       move /Y "output.tmp" "%%a" > NUL
    
    )
    

    This program should run faster than other methods that compare line by line; however, it has the disadvantage that leading spaces are removed from all lines. Although additional code may be inserted in order to fix this point, doing that will slow down the process...

提交回复
热议问题