我希望在文本文件中替换字符串(或附加到一个)。
我的文件中有很多该字符串的实例,但我只希望修改单个实例。为了识别这条线,我知道它总是会在包含唯一字符串的行下方找到。
我的想法是:
" cargo_mass:"
" base_color:"
" base_color: (1, 1, 1)"
答案 0 :(得分:5)
我想我可以尝试Aacini's method进行一些练习。请参阅内联注释以获取解释。将来,请展示您为解决问题所做的努力。
@echo off & setlocal
set "txtfile=notes.txt"
set "search=cargo_mass:"
set "nextline=base_color: (1, 1, 1)"
setlocal enabledelayedexpansion
rem // find number of line containing search string
for /f "delims=:" %%I in ('findstr /n /i "!search!" ^<"!txtfile!"') do set /a "num=%%I"
if not defined num ( echo(!search! not found in !txtfile! & goto :EOF )
rem // read txtfile
< "!txtfile!" (
rem // from beginning to found line, output each line untouched
for /L %%I in (1,1,%num%) do (
set line=
set /P "line="
echo(!line!
)
rem // consume a line of input and output the replacement txt
set /P "="
echo(!nextline!
rem // output the rest of the file
findstr "^"
) > "~!txtfile!"
move /y "~!txtfile!" "!txtfile!"