这是一个文本文件txtfile.txt
line1
line2
add after this line
line4
etc
etc
我想创建一个批处理文件,可以在add after this line
之后添加一行,并输入用户输入的信息。保持add after this line
完好无损。
示例:
enter info: asdf
,文件变为
line1
line2
add after this line
asdf
line4
etc
etc
我认为基本过程是循环复制它的文件,当我找到该行时,添加该行然后循环其余部分。我想知道怎么做。
答案 0 :(得分:4)
@ECHO OFF
SETLOCAL
SET /p info="enter info : "
:: read addafter line
(
FOR /f "delims=" %%i IN (poison1.txt) DO (
SET addafter=%%i
FOR /f "delims=" %%n IN (' findstr /n "^" txtfile.txt') DO (
SET line=%%n
SETLOCAL ENABLEDELAYEDEXPANSION
SET line=!line:*:=!
ECHO(!line!
IF "!line!"=="!addafter!" ECHO(%info%
ENDLOCAL
)
)
)>newfile.txt
FC newfile.txt txtfile.txt
GOTO :eof
其中poison1.txt
包含一行
"A line !of! ] many < & >var*ied %poison ^ char;ac(ters) | like "," a\nd+so=on"
和txtfile.txt包含此行。
poison1.txt
文件到addafter
FINDSTR
添加)完成!