将几个字符串转换为一个字符串
Convert few strings to one string
我有许多 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 标签,但我不知道如何将不同的字符串连接成一个。
@回声关闭
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
结果我有:
Information
2015-12-22T16:18:17
1CV8
Information
2015-12-23T16:18:17
1CV28
而且我还希望输出文件的名称与输入文件相同。
也许有人可以阻止我解决它?
你很接近:
@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"
)
我有许多 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 标签,但我不知道如何将不同的字符串连接成一个。
@回声关闭 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
结果我有:
Information
2015-12-22T16:18:17
1CV8
Information
2015-12-23T16:18:17
1CV28
而且我还希望输出文件的名称与输入文件相同。 也许有人可以阻止我解决它?
你很接近:
@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"
)