批处理 - 如果语句导致错误

时间:2015-06-24 06:54:35

标签: windows string parsing batch-file xml-parsing

我正在解析一个xml文件,其格式如下:

<ResourcePicture Name="a.jpg">
    <GeneratedPicture Name="b.jpg"/>            
    <GeneratedPicture Name="c.jpg"/>
</ResourcePicture>

我可以打印图片的每个名称,但我想在那里检查带有if语句的内容。但是我得到一个错误&#34;&lt;在这个时候出人意料。&#34;但是当我删除if语句时,效果一样好。那我错过了什么?提前谢谢。

set "xmlFile=pictures.xml"

REM split xml file into lines
for /f "tokens=* skip=2" %%a in (%xmlFile%) do (

    REM %%a contais a string like <ResourcePicture Name="a.jpg">    
    set "currnetLine=%%a"
    set "currnetLine=!currnetLine:"=+!"
    set counter=0

    for /f "delims=+ tokens=2" %%c in ("!currnetLine!") do (    

        REM %%c contains the picture name like a.jpg
        set /a counter=counter+1

        REM CHECKING SOMETHING
        if not %counter%==1 ( echo abc )

        echo %%c
    )
)

1 个答案:

答案 0 :(得分:1)

你需要delayed expansion(我不知道脚本的逻辑是否能达到你想要的效果,但是它没有错误):

@echo off
set "xmlFile=pictures.xml"

REM split xml file into lines
setlocal enableDelayedExpansion
for /f "tokens=* skip=2" %%a in (%xmlFile%) do (

    REM %%a contais a string like <ResourcePicture Name="a.jpg">    
    set "currnetLine=%%a"
    set "currnetLine=!currnetLine:"=+!"
    set counter=0

    for /f "delims=+ tokens=2" %%c in ("!currnetLine!") do (    

        REM %%c contains the picture name like a.jpg
        set /a counter=counter+1

        REM CHECKING SOMETHING
        if not !counter! == 1 ( echo abc )

        echo %%c
    )
)