簡體   English   中英

使用批處理在特定單詞之后插入文本

[英]Insert text after specific word using batch

我有一系列XML文件,我需要在其中添加幾行文本,但是附加文本的位置非常重要,否則XML文件將不再起作用。

幸運的是,需要在文本<Tools>之后插入新文本,該文本在每個XML文件中僅出現一次。

如何使用Windows批處理,找到文本<Tools>然后在其后直接插入其他文本? (為了論證,可以說要添加的文本是<HELLO WORLD>

親切的問候

編輯:

我只是意識到我需要將<Tools>所有實例替換為<Tools> <HELLO WORLD>

我怎樣才能做到這一點?

編輯2:

以下是vbs,它很好用,但不帶引號

有沒有一種方法可以用另一個文件的文本內容替換文本

    Const ForReading = 1
Const ForWriting = 2

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForReading)

strText = objFile.ReadAll
objFile.Close
strNewText = Replace(strText, "CH1", "*901*")

Set objFile = objFSO.OpenTextFile("C:\Text.txt", ForWriting)
objFile.WriteLine strNewText
objFile.Close 

引起問題的報價在哪里起作用?

有沒有一種方法可以用另一個文件的文本內容替換文本? 您是說要用另一個文件中的片段替換某些片段?

下面將在源文件中找到字符串,並輸出帶有插入內容的新文件。

:: Hide Command and Isolate Scope with Command Extensions
@echo off
setlocal EnableExtensions DisableDelayedExpansion

:: See the following for help
rem echo /?
rem setlocal /?
rem for /?
rem type /?
rem find /?
rem del /?


:: Setup
set "xIn=Test.xml"
set "xOut=Output.xml"
set "xTerm=<Tools>"
set "xInsert=^<HELLO WORLD^>"


:: Validate
if not defined xIn goto End
if not defined xOut goto End


:: Place on Seperate Line Method
if defined xOut if exist "%xOut%" del /f /q "%xOut%" > nul
for /f "usebackq tokens=*" %%a in (`type %xIn%`) do (
    echo.%%a >> %xOut%
    for /f "usebackq tokens=*" %%x in (`echo."%%a" ^| find "%xTerm%"`) do (
        echo.%xInsert% >> %xOut%
    )
)


:: Setup
set "xOut=Output2.xml"


:: Validate
if not defined xOut goto End


:: Place on the Same Line Method
if defined xOut if exist "%xOut%" del /f /q "%xOut%" > nul
for /f "usebackq tokens=*" %%a in (`type %xIn%`) do (
    for /f "usebackq tokens=*" %%x in (`echo."%%a" ^| find /v "%xTerm%"`) do (
        echo.%%a >> %xOut%
    )
    for /f "usebackq tokens=*" %%x in (`echo."%%a" ^| find "%xTerm%"`) do (
        echo.%%a %xInsert% >> %xOut%
    )
)

:End
endlocal

編輯:這是上述腳本的修改版本,它將用文件內容替換包含搜索詞的行。 它應該處理好報價。 請參閱腳本中的call命令以了解如何使用它。

:: Hide Command
@echo off

set "xResult="
call :ReplaceWith xResult "Source.xml" "<Tools>" "Input.xml" "Output.xml"
echo.%xResult%

goto End


::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:ReplaceWith <xReturn> <Source File> <Search Term> <Input File> <Output File>
:: Replace a line in the source file containing the search term with the entire
:: contents of the input file and save the changes to the output file.
:::: Note: Only supports single line search terms.
:: Returns the number of lines replaced.  -1 for error.

:: Hide Commands, Isolate Scope, and Set Abilities
@echo off
setlocal EnableExtensions DisableDelayedExpansion

:: Setup
set "xResult=-1"
set "xSource=%~2"
set "xTerm=%~3"
set "xInput=%~4"
set "xOut=%~5"

:: Validate
if not defined xSource goto EndReplaceWith
if not exist "%xSource%" goto EndReplaceWith
if not defined xTerm goto EndReplaceWith
if not defined xInput goto EndReplaceWith
if not exist "%xInput%" goto EndReplaceWith
set "xResult=0"

:: Search
type nul > %xOut%
for /f "usebackq tokens=* delims=" %%a in (`type "%xSource%"`) do (
    for /f "usebackq tokens=*" %%x in (`echo."%%a" ^| find /v "%xTerm%"`) do (
        echo.%%a>> %xOut%
    )
    for /f "usebackq tokens=*" %%x in (`echo."%%a" ^| find "%xTerm%"`) do (
        type "%xInput%" >> %xOut%
        echo.>> %xOut%
        set /a "xResult+=1"
    )
)
:EndReplaceWith
endlocal & if not "%~1"=="" set "%~1=%xResult%"
goto :eof
:: by David Ruhmann


:End
endlocal

使用或構造XSLT並應用它來保持XML有效會更安全嗎? 如果您願意探索此選項,我建議您在批處理文件中使用例如AltovaXML之類的命令行xslt處理器(免費社區版)。
許多IDE /代碼編輯器,例如Visual Studio / Notepad ++ / Emacs / Eclipse /…,也具有應用甚至調試XSLT(有時是集成的,有時使用插件或附加組件)的方法,在編寫轉換時可能會很方便。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM