Batch Insert Element from XML with Shared Filename

后端 未结 3 1516
春和景丽
春和景丽 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 06:46

    The batch environment isn't terribly strongly suited for manipulating XML as XML. There's probably a way using Windows Script Host (VBScript or JScript) to evaluate the XML DOM, but in this situation it's probably easier just to use for loops and echos.

    Read the remarks in the following example script for a full explanation of how it works.

    @echo off
    setlocal
    
    set "remarkDir=remarks\"
    set "xmlDir=xml\"
    
    rem // for all files in xmlDir\*.xml
    for %%I in ("%xmlDir%\*.xml") do (
    
        rem // echo filename without line break...
        set /P "=Processing %%~nxI... "<NUL
    
        rem // Read corresponding remark file into variable
        set /P "remark=" <"%remarkDir%\%%~nxI"
    
        rem // for each line in xmlDir\file.xml
        for /f "usebackq delims=" %%X in ("%%~fI") do (
    
            rem // append the line to a new file
            >>"%%~dpnI.new" echo/%%X
    
            rem // check whether the line contains /CHANGETIME
            set "line=%%X"
            setlocal enabledelayedexpansion
            if not "%%X"=="!line:/CHANGETIME=!" (
    
                rem // Line contains /CHANGETIME.  Append remark.
                >>"%%~dpnI.new" echo/!remark!
            )
            endlocal
    
        )
    
        rem // End of xml file.  Replace old with new.
        move /y "%%~dpnI.new" "%%~fI" >NUL
        echo Done.
    )
    

    note: StackOverflow isn't intended to be a free coding service, but I have sympathy for you. It sounds like you've put a lot of effort into painting yourself into this corner. Therefore, I hope this helps you out.

    0 讨论(0)
  • 2020-12-12 06:52

    Excuse me. In my first answer I said I wanted to use this problem as a test because its interesting aspect. Some time ago I wrote FilePointer.exe auxiliary program that allows to move the file pointer of a redirected file via its standard handle. That program may be used to solve this problem in a very simple way (and also any other problem with similar structure) because the former method of copy several lines via FOR command may be changed by a direct file pointer movement to a certain file position, or by a simple FINDSTR command to copy the rest of lines. Here it is:

    @echo off
    setlocal EnableDelayedExpansion
    
    rem Example of use of FilePointer.exe auxiliary program
    rem Antonio Perez Ayala
    
    rem Process all .xml files in current directory
    for %%a in (*.xml) do (
    
       rem Locate the insertion offset where "FILESIZE" line starts
       for /F "delims=:" %%b in ('findstr /O "FILESIZE" "%%a"') do set "insertPoint=%%b"
    
       rem Block used to edit the file via redirected Stdin and Stdout
       < "%%a" (
    
          rem Set Stdin file pointer at the insertion point
          FilePointer 0 !insertPoint!
    
          rem Copy the rest of lines to an auxiliary file
          findstr "^" > auxiliary.tmp 
    
          rem "FIND and MORE works different than FINDSTR."
          rem "FIND and MORE first resets the file position variable and then read the complete file to the EOF,"
          rem "If you use FINDSTR it simply reads the next data from current position, ..."
          rem http://www.dostips.com/forum/viewtopic.php?f=3&t=2128&p=9720#p9720
    
          rem Set Stdout file pointer at the insertion point
          FilePointer 1 !insertPoint!
    
          rem Insert the corresponding REMARK file
          type "RemarksFolder\%%a"
    
          rem And add the rest of lines
          type auxiliary.tmp
    
       ) >> "%%a"
       rem Block-end
    
    )
    
    del auxiliary.tmp
    

    This method have several advantages over the former one. It run faster and the leading spaces are preserved. The first part of the file is kept in the same file, that is, it does not need to copy it into a temporary file. In this problem it is necessary to copy the lines from the insertion point to the EOF in a temporary file in order to make room for the inserted text, but in another problem that just needs to replace a text by other one of the same size, the change is immediate with no further processing no matter the size of the file! If the new text would be shorter than the original one it would be necessary to "compact" the data after the replacement point and then truncate the remaining data, that may be done with Truncate.exe (another one of my auxiliary programs).

    You may download FilePointer.exe auxiliary program from this site.

    0 讨论(0)
  • 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...

    0 讨论(0)
提交回复
热议问题