如何使用批处理脚本将文本文件中的字符串写入XML文件?

时间:2019-12-02 06:33:53

标签: windows batch-file

我有一个文本文件,其中包含如下所示的msi日志

=== Logging started: 11/29/2019  15:27:45 ===
Action start 15:27:45: INSTALL.
Action start 15:27:45: FindRelatedProducts.
Action ended 15:27:45: FindRelatedProducts. Return value 1.
Action start 15:27:45: ValidateProductID.
Action ended 15:27:45: ValidateProductID. Return value 1.
Action start 15:27:45: CostInitialize.
Property(N): UpgradeCode = {x}
Property(N): TARGETDIR = E:\
Property(N): INSTALLDIR = C:\Program Files (x86)\Temp\
Property(N): ProgramFilesFolder = C:\Program Files (x86)\
Property(N): PASSWORD = x
Property(N): ARPSYSTEMCOMPONENT = 123 
Property(N): Location = "xyz"
=== Logging stopped: 11/29/2019  15:28:45 ===

我想使用批处理脚本将Location的值写入XML文件到称为location的标记中 我本人从未使用过批处理脚本,因此请提供任何帮助。

1 个答案:

答案 0 :(得分:2)

这将起作用:

@echo off
echo ^<xml^> >script.xml
for /f "tokens=4 delims= " %%a in ('type [the name of your msi log txt file].txt ^| find "Location"') do (echo ^<location^>%%a^</location^> >>script.xml)
echo ^</xml^> >>script.xml
goto :eof

用您自己的替换“ [[您的msi日志txt文件的名称]”。它将使用数据创建一个名为script.xml的XML文件。 如果您在该位置有更多单词,请查看批处理文件中的tokens字段,写一个逗号并输入5(如果您有更多这样的单词,则数字5会随着您的增加而增加)。 / p>