string - 通过Windows批处理文件将文件中的字符串 append 到另一个字符串之后

标签 string windows file batch-file append

我正在尝试编写一个批处理文件,以将某个字符串“str2 => bbb” append 到文件中(如果文件中尚不存在)。 “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
) 

但是我不知道该怎么做才能在下一行中写另一个字符串。

最佳答案

由于我不清楚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是否出现多次。

关于string - 通过Windows批处理文件将文件中的字符串 append 到另一个字符串之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39349420/

相关文章:

python - 从python中的随机列表中选择时出错

c# - 在 Windows 中,当我在 Windows 资源管理器中双击文件时会发生什么?

c# - 如何更新 C# Windows 控制台应用程序中的当前行?

file - 您可以在 Outlook 以外的任何软件中创建常用文件吗?

python - 文件未写入当前文件位置

java - 为什么这段代码会进入我的方法的错误 block ?

C++ 将多个字符串更改为一个枚举

php - 如何从 PHP 中存储在我的数据库中的文本中获取前 n 个单词?

windows - 站点到站点 VPN 与点到站点 VPN

java - 在Java中将从两个数据库提取的对象列表写入txt文件的最快方法