通过Windows批处理文件在另一个字符串后面的文件中追加一个字符串

时间:2016-09-06 12:48:54

标签: string windows file batch-file append

我正在尝试编写批处理文件,以便将某个字符串“str2 => bbb”附加到文件中(如果该文件中尚不存在)。 “str2”将在字符串“str1 => aaa”之后(它始终存在于文件中)。例如:

file.txt的

...

str1 => AAA

...

file.txt的结尾

它将成为:

file.txt的

...

...

...

str1 => AAA

str2 => BBB

...

file.txt的结尾

并且批处理文件必须不具有破坏性,即如果文件中已存在“str2”,则批处理将不执行任何操作。

我知道如何在文件中找到一个字符串:

FINDSTR "str2 => bbb" "file.txt"

IF %errorlevel%==0 (
    ECHO FOUND
) 

但我不知道在下一行写另一个字符串还能做些什么。

2 个答案:

答案 0 :(得分:1)

由于我不清楚str2是否必须在文件中str1之后立即发生,或者只是在任何地方,我编写了以下脚本,它能够涵盖两个标准。它会直接修改输入文件,所以要小心。输入文件必须作为命令行参数:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constants here:
set "FILE=%~1" & rem // (input file; `%~1` takes first command line argument)
set "WORD1=str1" & rem // (word after which `%WORD2%` must be inserted)
set "WORD2=str2" & rem // (word that must be present in the file)
set "STRING2=%WORD2% => bbb" & rem // (full string to insert if `%WORD2%` is missing)
set "SEPARATORS=    = " & rem // (characters that separate the words from the rest)
set "FIXEDPOS=#" & rem // (if not empty, defines that `%WORD2%` must be after `%WORD1%`)

rem // Create line-break (carriage-return and line-feed):
(for /F %%# in ('copy /Z "%~f0" nul') do set ^"CR+LF=%%#^
%= empty line =%
^")

rem // Ensure list of separators contains (ends) with space:
if defined SEPARATORS (
    if not "%SEPARATORS:~-1%"==" " set "SEPARATORS=%SEPARATORS: =% "
) else set "SEPARATORS= "
setlocal EnableDelayedExpansion
rem // Set up regular expression:
if defined FIXEDPOS (
    rem /* `%WORD2%` must be in the line following `%WORD1%`, so define a dual-line
    rem    regular expression (both words must be present at the beginnings of lines): */
    set "REGEX=^%WORD1%[%SEPARATORS%].*!CR+LF!%WORD2%[%SEPARATORS%]"
) else (
    rem /* Position of `%WORD2%` does not matter with respect to `%WORD1%`,
    rem    hence it merely must be present at the beginning of a line: */
    set "REGEX=^%WORD2%[%SEPARATORS%]"
)
rem // Search for regular expression in file:
> nul findstr /I /R /C:"!REGEX!" "%FILE%" || (
    rem // No match encountered, so read entire file and deplete it afterwards:
    for /F "delims=" %%L in ('findstr /N /R "^" "%FILE%" ^& ^> "%FILE%" break') do (
        endlocal
        rem // Read a line, reset flag that defines whether or not to insert a string:
        set "FLAG=" & set "LINE=%%L"
        setlocal EnableDelayedExpansion
        rem // Split off first word and compare with `%WORD1%`:
        for /F "eol=  tokens=1 delims=%SEPARATORS%" %%K in ("!LINE:*:=!") do (
            endlocal
            rem // First word matches `%WORD1%`, so set flag:
            if /I "%%K"=="%WORD1%" set "FLAG=#"
            setlocal EnableDelayedExpansion
        )
        rem // Append to file:
        >> "%FILE%" (
            rem // Write original line:
            echo(!LINE:*:=!
            rem // Write string to insert in case flag is defined:
            if defined FLAG echo(!STRING2!
        )
    )
)
endlocal

endlocal
exit /B

请注意,此脚本不会检查str1是否多次出现。

答案 1 :(得分:0)

在批处理文件中使用powershell来简化操作

{{1}}

powershell命令将获取文件的内容并用搜索字符串($&)+ new line + str2替换你的字符串......)