Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

Using a .bat script, I want to find a line that says # Site 1 and replace the text in the next line with a variable. I found tutorials on StackOverflow for finding and replacing a line, but not finding a line and replacing the next line. Any help?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.1k views
Welcome To Ask or Share your Answers For Others

1 Answer

@echo off

set "the_file=C:someFile"
set "search_for=somestring"
set "variable=http://site1"

for /f "tokens=1 delims=:" %%# in ('findstr /n  /c:"%search_for%" "%the_file%"') do (
    set "line=%%#"
    goto :break
)
:break


set /a lineBefore=line-1
set /a nextLine=line+1


break>"%temp%empty"&&fc "%temp%empty" "%the_file%" /lb  %lineBefore% /t |more +4 | findstr /B /E /V "*****" >newFile
echo %variable%>>newFile
more "%the_file%" +%nextLine% 1>>newFile

echo move /y newFile "%the_file%"

Check if newFile is ok and remove the echo at the front of the last line.

you need to set the three variables at the beginning by yourself.Have on mind that more command sets spaces instead of tabs


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...