批量不给出所需的输出

时间:2014-03-26 01:22:07

标签: xml batch-file

我有一个包含大约15,103个xml文件的文件夹。

该文件夹中的xml文件示例如下。

000010000.img.xml

我想要关注的xml部分的片段。

<imgdir name="000010000.img">
   <imgdir name="info">
      <int name="version" value="10" />
      <int name="cloud" value="0" />
      <int name="town" value="0" />
      <float name="mobRate" value="1.0" />
      <string name="bgm" value="Bgm34/MapleLeaf" />
      <int name="returnMap" value="10000" />
      <string name="mapDesc" value="" />
      <int name="hideMinimap" value="0" />
      <int name="forcedReturn" value="999999999" />
      <int name="moveLimit" value="0" />
      <string name="mapMark" value="MushroomVillage" />
      <int name="swim" value="0" />
      <int name="fieldLimit" value="8260" />
      <int name="VRTop" value="-892" />
      <int name="VRLeft" value="-1064" />
      <int name="VRBottom" value="915" />
      <int name="VRRight" value="1334" />
      <int name="fly" value="0" />
      <int name="noMapCmd" value="0" />
      <string name="onFirstUserEnter" value="" />
      <string name="onUserEnter" value="go10000" />
      <int name="standAlone" value="0" />
      <int name="partyStandAlone" value="0" />
      <string name="fieldScript" value="" />
   </imgdir>

   </imgdir>
   <imgdir name="portal">
      <imgdir name="0">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-389" />
         <int name="y" value="183" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="1">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-416" />
         <int name="y" value="185" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="2">
         <string name="pn" value="sp" />
         <int name="pt" value="0" />
         <int name="x" value="-450" />
         <int name="y" value="183" />
         <int name="tm" value="999999999" />
         <string name="tn" value="" />
      </imgdir>
      <imgdir name="3">
         <string name="pn" value="out00" />
         <int name="pt" value="2" />
         <int name="x" value="1080" />
         <int name="y" value="541" />
         <int name="tm" value="20000" />
         <string name="tn" value="in00" />
         <string name="script" value="" />
         <int name="hideTooltip" value="0" />
         <int name="onlyOnce" value="0" />
         <int name="delay" value="0" />
      </imgdir>
   </imgdir>

OUTPUT1:这是我想要的输出;

[10000]
total=4
sp 0 -389 183 999999999
sp 0 -416 185 999999999
sp 0 -450 183 999999999
out00 2 1080 541 20000

这是批处理代码;

@echo off
setlocal EnableDelayedExpansion

(for %%a in (*.xml) do call :processFile "%%a") > output.txt
goto :EOF


:processFile
set "returnMap="
for /F "tokens=3,5 delims==> " %%a in ('findstr /C:"<int name=" /C:"<imgdir name=" /C:"<string name=" %1') do (
   if not defined returnMap (
      if %%a equ "returnMap" (
         echo [%%~b]
         set returnMap=true
         set "portal="
      )
   ) else (
      if not defined portal (
         if %%a equ "portal" set portal=true & set /A i=0, skip=1
      ) else (
         if !skip! equ 1 (
            set /A skip-=1
            set "line="
         ) else if %%a neq "tn" (
            set "line=!line! %%~b"
         ) else (
            set /A i+=1, skip=1
            set "line[!i!]=!line:~1!"
            if %%b neq "" goto endPortals
         )
      )
   )
)

OUTPUT2:这是我在output.txt中得到的输出,而不是上面所需的输出;

C:\Users\Andrew\Desktop\New folder>call :processFile "000010000.img.xml" 

C:\Users\Andrew\Desktop\New folder>set "returnMap=" 

C:\Users\Andrew\Desktop\New folder>for /F "tokens=3,5 delims==> " %a in ('findstr /C:"<int name=" /C:"<imgdir name=" /C:"<string name=" "000010000.img.xml"') do (if not defined returnMap (if %a EQU "returnMap" (
echo [%~b]  
 set returnMap=true  
 set "portal=" 
) )  else (if not defined portal (if %a EQU "portal" set portal=true   & set /A i=0, skip=1 )  else (if !skip! EQU 1 (
set /A skip-=1  
 set "line=" 
)  else if %a NEQ "tn" (set "line=!line! %~b" )  else (
set /A i+=1, skip=1  
 set "line[!i!]=!line:~1!"  
 if %b NEQ "" goto endPortals 
) ) ) ) 

C:\Users\Andrew\Desktop\New folder>(if not defined returnMap (if "1.0" EQU "returnMap" (
echo [UTF-8]  
 set returnMap=true  
 set "portal=" 
) )  else (if not defined portal (if "1.0" EQU "portal" set portal=true   & set /A i=0, skip=1 )  else (if !skip! EQU 1 (
set /A skip-=1  
 set "line=" 
)  else if "1.0" NEQ "tn" (set "line=!line! UTF-8" )  else (
set /A i+=1, skip=1  
 set "line[!i!]=!line:~1!"  
 if "UTF-8" NEQ "" goto endPortals 
) ) ) ) 

C:\Users\Andrew\Desktop\New folder>echo total= 
total=

C:\Users\Andrew\Desktop\New folder>for /L %i in (1 1) do echo !line[%i]! 

有什么问题?

1 个答案:

答案 0 :(得分:0)

@echo off
setlocal EnableDelayedExpansion

(for %%z in (q22649719.t?t) do call :processFile "%%z") >output.txt
goto :EOF


:processFile
set "returnMap="
set "portal="
set /a i=0
for /F "tokens=3,5 delims==> " %%a in (
 'sed s/\x3e\x20\x3c/\x3e\x0d\x0a\x3c/g "%1" ^|findstr /C:"<int name=" /C:"<imgdir name=" /C:"<string name=" 2^>nul') do (
 if not defined returnMap (
  if %%a equ "returnMap" (
    set "returnMap=[%%~b]"
  )
 ) else (
  if not defined portal (
   if %%a equ "portal" set portal=true&SET /a i+=1&SET "line="
  ) else ( 
   FOR %%w IN (pn pt x y tm) DO IF %%a equ "%%w" SET "line=!line! %%~b"
   IF %%a equ "tn" SET "line[!i!]=!line:~1!"&SET /a i+=1&SET "line="
  )
 )
)
SET /a i-=1
ECHO %returnmap%
ECHO(total=%i%
FOR /l %%a IN (1,1,%i%) DO ECHO !line[%%a]!

GOTO :EOF

这似乎可以完成这项工作 - 我使用了一个名为q22649719.txt的文件,其中包含我的测试数据,生成

[10000]
total=4
sp 0 -389 183 999999999
sp 0 -416 185 999999999
sp 0 -450 183 999999999
out00 2 1080 541 20000