windows 使用批处理脚本在文本文件的特定行号处插入文本

5uzkadbs  于 2023-03-13  发布在  Windows
关注(0)|答案(2)|浏览(325)

我想插入一个字符串的文本说“你好”在一个特定的行说n(12或23或其他一些数字)使用批处理脚本。我知道批处理脚本是坏的,但不幸的是,这是批处理脚本的要求。
这是我试过的方法

@echo off
setlocal enableextensions disabledelayedexpansion
set "nthline=TEXT"
(for /f "usebackq tokens=* delims=" %%a in (c.txt) do (
    echo(%%a
    if defined nthline ( 
        echo(%nthline%
        set "nthline="
    )
))

这段代码我从这里得到,但它只插入到第二行我不能让它超过这一点。
下面是文本文件包含的内容

aaaa
bbbb
cccc
dddd
ffff
gggg
hhhh

它在aaaa之后插入字符串。我怎样才能使它在bbbb或ffff处插入?我是批处理脚本的新手,所以非常感谢任何帮助

nhjlsmyf

nhjlsmyf1#

您发布的代码旨在在第一行之后插入额外的一行。
要在某个点插入,您需要以某种方式预定义目标行号和行号,然后比较这些数字是否相等,并在以下脚本中返回额外的文本行-line:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constante here:
set "_FILE=c.txt" & rem // (text file to insert a line of text into)
set "_TEXT=hello" & rem // (text of the inserted line)
set /A "_NUM=4"   & rem // (number of the line where the new line is inserted)
set "_REPLAC=#"   & rem // (set to anything to replace the target line, or to nothing to keep it)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path and name of temporary file)

rem // Write result into temporary file:
> "%_TMPF%" (
    rem // Loop through all lines of the text file, each with a preceding line number:
    for /F "delims=" %%L in ('findstr /N "^" "%_FILE%"') do (
        rem // Store current line including preceding line number to a variable:
        set "LINE=%%L"
        rem // Set the current line number to another variable:
        set /A "LNUM=%%L"
        rem // Toggle delayed expansion to avoid trouble with `!`:
        setlocal EnableDelayedExpansion
        rem // Compare current line number with predefined one:
        if !LMUN! equ %_NUM% (
            rem // Line numbers match, so return extra line of text:
            echo(!_TEXT!
            rem // Return original line (without preceding line number) only if it is not to be replaced:
            if not defined _REPLAC echo(!LINE:*:=!
        ) else (
            rem // Line numbers do not match, so return original line (without preceding line number) anyway:
            echo(!LINE:*:=!
        )
        endlocal
    )
)
rem // Move temporary file onto original one:
move /Y "%_TMPF%" "%_FILE%"

endlocal
exit /B
2cmtqfgy

2cmtqfgy2#

对ASchipfl解决方案的一些修正:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

rem // Define constante here:
set "_FILE=c.txt" & rem // (text file to insert a line of text into)
set "_TEXT=hello" & rem // (text of the inserted line)
set /A "_NUM=4"   & rem // (number of the line where the new line is inserted)
set "_TMPF=%TEMP%\%~n0_%RANDOM%.tmp" & rem // (path and name of temporary file)

rem // Write result into temporary file:
> "%_TMPF%" (
    rem // Loop through all lines of the text file, each with a preceding line number:
    for /F "usebackq delims=" %%L in (`findstr /N "^" "%_FILE%"`) do (
        rem // Store current line including preceding line number to a variable:
        set "LINE=%%L"
        rem // Set the current line number to another variable:
        set /A "LNUM=%%L"
        rem // Toggle delayed expansion to avoid trouble with `!`:
        setlocal EnableDelayedExpansion
        rem // Compare current line number with predefined one:
        if !LNUM! equ %_NUM% (
            rem // Line numbers match, so return extra line of text:
            echo !_TEXT!
        ) else (
            rem // Line numbers do not match, so return original line (without preceding line number) anyway:
            rem // remove the left leading spaces
            for /f "tokens=* delims= " %%s in ("!LINE:*:=!") do set tl=%%s
            if "!tl!" == "" (
                echo:
            ) else (
                echo !LINE:*:=!
            )

        )
        endlocal
    )
)
rem // Move temporary file onto original one:
move /Y "%_TMPF%" "%_FILE%"

endlocal
exit /B

可以说,这是一个稍微简单一点的工作版本。注意for/loop中的反勾号

相关问题