使用批处理脚本将文本文件转换为xml标记格式

时间:2014-11-27 19:00:28

标签: batch-file scripting

我的文字文件位于以下内容之下。

U    /modules/configuration/gsrc/util/CoverageValidation.gs

C    /modules/configuration/gsrc/util/CoverageForm.gs

D    /modules/configuration/gsrc/util/Trigger.gs

A    /modules/configuration/gsrc/util/Trigger-join.gs

我的脚本是这样的:

@echo off
    setlocal enabledelayedexpansion

    set file=test.txt
    set update_tag=<action>Updated</action>
    set add_tag=<action>Added</action>
    set conflict_tag=<action>Conflicted</action>
    set delete_tag=<action>Deleted</action>
    if exist !file! (
        echo The file exists.
        for /f "tokens=* delims=" %%a in (!file!) do (
            set line=%%a
            if !line! equ "U    " (
                set line=~0,5=!update_tag!!
            )else if !line! equ "A    " (
                set line=~0,5=!add_tag!!
            )else if !line! equ "C    " (
                set line=~0,5=!conflict_tag!!
            )else if !line! equ "D    " (
                set line=~0,5=!delete_tag!!
            )
        )
    ) else (
        echo The file doesn't exist.
    )
endlocal

现在我需要使用Batch脚本将此文本文件转换为以下格式。

<action>Updated</action>

<path>/modules/configuration/gsrc/util/CoverageValidation.gs</path>

<action>Conflicted</action>

<path>/modules/configuration/gsrc/util/CoverageForm.gs</path>

<action>Deleted</action>

<path>/modules/configuration/gsrc/util/Trigger.gs</path>

<action>Added</action>

<path>/modules/configuration/gsrc/util/Trigger-join.gs</path>

1 个答案:

答案 0 :(得分:0)

type file.txt |repl "^U " "Updated\r\n\r\n" x|repl "^C " "Conflicted\r\n\r\n" x|repl "^D " "Deleted\r\n\r\n" x|repl "^A " "Added\r\n\r\n" x >file2.txt

这使用名为repl.bat的助手批处理文件(由dbenham提供) - 从以下网址下载:https://www.dropbox.com/s/qidqwztmetbvklt/repl.bat

repl.bat放在与批处理文件相同的文件夹中或放在路径上的文件夹中。