我正在parsing一个xml文件,它的格式是这样的:
<ResourcePicture Name="a.jpg"> <GeneratedPicture Name="b.jpg"/> <GeneratedPicture Name="c.jpg"/> </ResourcePicture>
我可以打印每个图片的名字,但我想检查一下if语句里面的东西。 但是我得到一个错误“<此时是意外的”。 但是当我删除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 ) )
你需要延迟扩展 (我不知道脚本的逻辑是否达到你想要的,但这样就没有错误):
@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 ) )