2015-12-24 248 views
0

我有许多具有许多不同事件的XML文件。任何事件有一些标签:将几个字符串转换为一个字符串

<v8e:Event> 
    <v8e:Level>Information</v8e:Level> 
    <v8e:Date>2015-12-22T16:18:17</v8e:Date> 
    <v8e:ApplicationName>1CV8</v8e:ApplicationName> 
</v8e:Event> 
<v8e:Event> 
    <v8e:Level>Information</v8e:Level> 
    <v8e:Date>2015-12-23T16:18:17</v8e:Date> 
    <v8e:ApplicationName>1CV28</v8e:ApplicationName> 
</v8e:Event> 

我想这个转换为狭窄此:

Information, 2015-12-22T16:18:17, 1CV8 
Information, 2015-12-23T16:18:17, 1CV28 

我知道如何删除XML标记,但我不是不知道如何串连不同的字符串中的一个。

关闭@echo SETLOCAL EnableDelayedExpansion

(for /F "delims=" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" *.xml') do (
    set "line=%%a" 
    set "line=!line:*<v8e:Level>=!" 
    set "line=!line:*<v8e:Date>=!" 
    set "line=!line:*<v8e:ApplicationName>=!" 
    for /F "delims=<" %%b in ("!line!") do echo %%b 
)) > 111.txt 

由于reuslt我:

Information 
2015-12-22T16:18:17 
1CV8 
Information 
2015-12-23T16:18:17 
1CV28 

而且我也想输出文件同样喜欢输入文件名。 也许有人可以让我解决它?

回答

1

你接近:

@echo off 
setlocal EnableDelayedExpansion 

set "output=" 
set "fields=0" 
for /F "delims=" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" *.xml') do (
    set "line=%%a" 
    set "line=!line:*<v8e:Level>=!" 
    set "line=!line:*<v8e:Date>=!" 
    set "line=!line:*<v8e:ApplicationName>=!" 
    for /F "delims=<" %%b in ("!line!") do set "line=%%b" 
    set "output=!output!!line!, " 
    set /A fields+=1 
    if !fields! equ 3 (
     set "file=%%a" 
     for /F "delims=:" %%b in ("!file!") do echo !output:~0,-2!>> "%%~Nb.txt" 
     set "output=" 
     set "fields=0" 
    ) 
) 

然而,这是我会做的方式:

@echo off 
setlocal EnableDelayedExpansion 

for %%f in (*.xml) do (

    set "output=" 
    set "fields=0" 
    (for /F "tokens=3 delims=<>" %%a in ('findstr /I /L "<v8e:Level> <v8e:Date> <v8e:ApplicationName>" "%%f"') do (
     set "output=!output!%%a, " 
     set /A fields+=1 
     if !fields! equ 3 (
     echo !output:~0,-2! 
     set "output=" 
     set "fields=0" 
    ) 
    )) > "%%~Nf.txt" 

) 
+0

如果我想改变我的申请数量?对于Exampe add extra 7 filed:,Ialso应该改变在你的脚本?如果某个领域是空的,我该怎么做?例如 信息 2015-12-23T16:18:17 hubba900

+0

添加额外的7个字段:并向3个字段添加7。如果某个字段为空,则可以使用标记= 2,3而不是3,如果“%% b”neq“”(设置“output =!output!%% b,”)else(set“output =!output!empty ,“)而是只设置”output =!output!%% a“, – Aacini