2015-12-24 246 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