2014-09-30 83 views
0

我想創建一個批處理文件,比較存儲在兩個不同位置的單個XML文件的兩個副本,並檢查它們的版本是否相同。使用Windows批量比較XML文件的多個標記

目前我正在使用fc & findstr命令只檢查2個文件中的構建標記,但我想添加代碼1st檢查主標記的功能,如果它不同,則代碼停止執行並且打印「不同的文件」。

如果值相同,則繼續檢查2個文件中的次標籤。如果值不同,代碼將停止執​​行並打印「文件不同」。如果值相同,則繼續檢查構建標記。如果構建值不同,則它打印「Files different」或「files same」。

因此,檢查標記的流程是 - > - >

貯存於2個的不同位置的文件是

TData.xml

<?xml version="1.0" encoding="UTF-8"?> 
<CDMDataXML xmlns="http://www.avocent.org/trellis/CDMLoaderXMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.avocent.org/trellis/CDMLoaderXMLSchema CDMLoaderXMLSchema.xsd"> 
<CDMDataVersion> 
    <Major>3</Major> 
    <Minor>0</Minor> 
    <Build>19</Build> 
    <Delimiter>.</Delimiter> 
</CDMDataVersion> 

我使用的批次代碼是..

@echo off 
    fc D:\SVN\TData\TData.xml Z:\TDataGDDFolders\TData.xml /A > nul 

if errorlevel 1 (
    echo. 
    echo in SVN 
    findstr "<Build>" D:\SVN\TData\TData.xml 
    echo. 
    echo in DropBox 
    findstr "<Build>" Z:\TDataGDDFolders\TData.xml 
    echo. 
    echo. 
    echo TData files are different. 
) else (
    echo. 
    echo in SVN 
    findstr "<Build>" D:\SVN\TData\TData.xml 
    echo. 
    echo in DropBox 
    findstr "<Build>" Z:\TDataGDDFolders\TData.xml 
    echo. 
    echo. 
    echo TData files matches.  
) 

我想在批處理代碼添加上述功能,但似乎無法理解怎麼辦呢?請幫助..

回答

1

剛剛從previous answer

@echo off 
    setlocal enableextensions disabledelayedexpansion 

    set "file1=%cd%\tdata_1.xml" 
    set "file2=%cd%\tdata_2.xml" 

    call :compareXML "%file1%" "%file2%" "Major" || (echo files different & goto :eof) 
    call :compareXML "%file1%" "%file2%" "Minor" || (echo files different & goto :eof) 
    call :compareXML "%file1%" "%file2%" "Build" || (echo files different & goto :eof) 

    echo files same 

    exit /b 

:compareXML file1 file2 taglist 
    setlocal enableextensions disabledelayedexpansion 

    setlocal enableextensions enabledelayedexpansion 
    set "match=" & for %%a in (%~3) do set "match=!match! /c:"^<%%a^>"" 
    endlocal & set match=%match% 

    for /f "tokens=1,2 delims=<> " %%a in (' 
     type "%~1" "%~2" 2^> nul ^| findstr /i /l %match% 
    ') do if not defined _F1_%%a (set "_F1_%%a=%%b") else (
     setlocal enabledelayedexpansion 
     for %%c in ("!_F1_%%a!.") do (
      endlocal 
      if /i not "%%b."=="%%~c" (endlocal & exit /b 1) 
      set "_F1_%%a=" 
     ) 
    ) 
    set _F1_ 2>nul && (endlocal & exit /b 1) || (endlocal & exit /b 0) 
重用代碼
+0

如果我想添加像「D:\ SVN \ TData \ TData.xml」的完整路徑,我應該將它添加到%cd%或其外部嗎? – Lucy 2014-10-01 06:29:49

+0

@Lucy,代替'%cd%'。直接使用'「set file1 = D:\ SVN \ TData \ TData.xml」'。它僅僅是爲了顯示文件引用可以包含一個路徑(示例中的當前目錄) – 2014-10-01 06:32:57

+0

它正在工作..非常感謝! – Lucy 2014-10-06 06:48:15