@echo off
setlocal EnableDelayedExpansion
(for %%z in (*.xml) 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 ('findstr /C:"<int name=" /C:"<imgdir name=" /C:"<string name=" %1') 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
我在一个文件夹中有大约1.5万个xml文件。
上面的代码应该遍历每个xml文件,并创建一个包含列表中所有信息的文本文件;
[10000]
total=4
sp 0 -389 183 999999999
sp 0 -416 185 999999999
sp 0 -450 183 999999999
out00 2 1080 541 20000
[3234435]
total=6
...etc...
但是,当我在文件夹中运行批处理脚本时,我在output.txt中得到的输出如下;
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
total=-1
ECHO is off.
帮助。
我在上一个帖子@ Batch not giving desired output
中引用了xml的代码段示例答案 0 :(得分:0)
你可以测试一下:
@ECHO OFF &SETLOCAL enableDelayedExpansion
SET "FName=file.xml"
SET "Portal="
SET "Imgdir="
SET /a xcnt=0, ycnt=0, zcnt=-1
FOR /f "tokens=2delims=<>" %%a IN ('findstr /i /c:"name=\"portal\"" /c:"name=\"pn\"" /c:"name=\"pt\"" /c:"name=\"x\"" /c:"name=\"y\"" /c:"name=\"tm\"" /c:"imgdir" /c:"returnMap" "%FName%"') DO (
FOR /f tokens^=1-2^,4delims^=^" %%b IN ("%%~a") DO (
IF /i "%%~c"=="returnMap" ECHO([%%~d]
IF DEFINED Portal (
IF DEFINED Imgdir IF NOT "%%~c"=="" SET "val.!xcnt!.!ycnt!=%%~d"&SET /a ycnt+=1
IF /i "%%~b"=="/imgdir" (
IF NOT DEFINED Imgdir (
SET "Portal="
) ELSE (
SET "Imgdir="
SET /a xcnt+=1, ycnt=0, zcnt+=1
)
)
IF /i "%%~b"=="imgdir name=" SET "Imgdir=7"
)
IF /i "%%~c"=="portal" SET "Portal=7"
)
)
ECHO(total=%xcnt%
FOR /l %%a IN (0, 1, %zcnt%) DO (
FOR /f "tokens=1*delims==" %%b IN ('SET val.%%a') DO <nul SET /p "=%%c "
ECHO(
)
输出是:
[10000] total=4 sp 0 -389 183 999999999 sp 0 -416 185 999999999 sp 0 -450 183 999999999 out00 2 1080 541 20000