I\'m trying to replace this line:
# forward-socks5 / 127.0.0.1:9050 .
with this one:
Lines containing !
are corrupted because delayed expansion occurs after FOR variable (%%A
) expansion. That could be solved by starting out with delayed expansion disabled. Within the loop you save the value of %%A
in a variable, and then toggle delayed expansion on, process the line, and then toggle it back off.
You do not need to expand the variable within a SET /A computation.
You can do everything in one step by adding an IF statement within your loop.
In fact, you don't even need SET /A or FINDSTR at all. Your IF statement can test if the line matches your search string. You don't really need delayed expansion for your problem.
It is more efficient to enclose the entire loop within parens and redirect to your output file just once.
@echo off
setlocal disableDelayedExpansion
:Variables
set InputFile=config.txt
set OutputFile=config-new.txt
set "_strFind=# forward-socks5 / 127.0.0.1:9050 ."
set "_strInsert= forward-socks5 / 127.0.0.1:9050 ."
:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
)
)