简体   繁体   English

使用Windows批处理比较两个XML文件

[英]Compare two XML files using Windows batch

I'm trying to create a batch file which reads an identical XML files stored in two different locations and compares whether their build version is the same or not using the build tag in the xmls file.The batch code is.. 我正在尝试创建一个批处理文件,该批处理文件读取存储在两个不同位置的相同XML文件,并使用xmls文件中的build标记比较它们的构建版本是否相同。批处理代码为..

Batch code is.. 批处理代码是..

 @echo off
setlocal enableextensions disabledelayedexpansion

set "build=" 
set "build1="
set "ans=version same"
set "wrong=version not same"
for /f "tokens=3 delims=<>" %%a %%b in (
    'find /i "<Build>" ^< "C:\Users\Lucy\Desktop\piller-uniblock\master.xml" "C:\piller-uniblock\master.xml" '
) do set "build=%%a" "build1=%%b"

IF "%build%"=="%build1%" echo %ans%
 else echo %wrong%

I'm trying to store the value of build tag of the two xml files in 2 variables, then compare them.If they are same then it prints "same" else "not same" through variables. 我试图将两个xml文件的build标签的值存储在2个变量中,然后进行比较。如果它们相同,则通过变量打印“相同”,否则显示“不相同”。

The XML file stored at two different locations which I'm trying to read is 我尝试读取的存储在两个不同位置的XML文件是

master.xml master.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>1</Major>
    <Minor>0</Minor>
    <Build>50</Build>
    <Delimiter>.</Delimiter>
</CDMDataVersion> 

The error message I'm getting is.. 我收到的错误消息是..

ERROR1

I can't understand where I'm making a mistake in this code.Please Help.. 我不明白我在此代码中的错误之处。请帮助。

type the two files, filter with find to get only the <build> lines and depending on which variable contains data, assign the retrieved information to one or the other type两个文件,使用find过滤以仅获取<build>行,并根据哪个变量包含数据,将检索到的信息分配给一个或另一个

@echo off
    setlocal enableextensions disabledelayedexpansion

    set "build=" 
    set "build1="

    set "ans=version same"
    set "wrong=version not same"

    for /f "tokens=3 delims=<>" %%a in ('
        2^>nul type "C:\Users\Lucy\Desktop\piller-uniblock\master.xml"
                    "C:\piller-uniblock\master.xml"
        ^| find /i "<Build>" 
    ') do if not defined build ( set "build=%%a" ) else ( set "build1=%%a" )

    IF "%build%"=="%build1%" ( echo %ans% ) else echo %wrong%

you error is here for /f "tokens=3 delims=<>" %%a %%b 您的错误是在for /f "tokens=3 delims=<>" %%a %%b

first your are taking only the third token so %%b will never be set. 首先,您只使用第三个令牌,因此将不会设置%% b。
Then you dont have to specify %%b even if you want more than one token that means : 然后,即使您需要多个令牌,也不必指定%% b,这意味着:
(say you want to get the third and fourth tokens) you can write : (例如,您想获取第三个和第四个令牌)可以编写:

for /f "tokens=3,4 delims=<>" %%a in ... do echo %%a %%b (%%b will be automatically populated with the 4th token) for /f "tokens=3,4 delims=<>" %%a in ... do echo %%a %%b (%% b将自动填充第四个令牌)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM