Batch: Replacing a line by another line
<p>I know similar questions had been asked many time, but I cannot workout something fitting my needs...</p>

<p>So I have a file, containing a secret key, the line is starting like :</p>

<pre><code>SECRET_KEY = 
</code></pre>

<p>The secret key is a non-fixed length.</p>

<p>I want to find this line, to save it somewhere, to replace the all file containing the secret key by another file (very similar, but with a different secret key which I don't want), and to reintroduce the original secret key in the new file!</p>

<p>So for the moment I have this code:</p>

<pre><code>echo Saving the actual SECRET_KEY...
findstr "SECRET_KEY" somefile.py &gt; SECRET_KEY.txt
echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY
</code></pre>

<p>This part is working, as I have stored the original secret key in a text file, and replaced the file by another (together with other files, directories structures, etc...)</p>

<p>So now how can I find again the line starting by SECRET_KEY and replace the all line by the content of SECRET_KEY.txt?</p>

<p>EDIT:
I made some progress I think:</p>

<pre><code>echo Saving the SECRET_KEY...
findstr "SECRET_KEY" inputfile.py &gt; SECRET_KEY.txt

echo Copying source code files...
xcopy %~dp0\somepath %~dp0\someotherpath /EY

echo Restoring the SECRET_KEY...
setlocal EnableDelayedExpansion
for /F "delims=" %%a in ('findstr /I "SECRET_KEY" SECRET_KEY.txt') do set "secret_key=%%a"
echo !secret_key!
@echo off
set file=inputfile.py
set newline=!secret_key!
set insertline=23
set output=outputfile.py
(for /f "tokens=1* delims=[]" %%a in ('find /n /v "##" ^&lt; "%file%"') do (
if "%%~a"=="%insertline%" (
echo !newline!
) ELSE (
echo.%%b
)
)) &gt; %output%
</code></pre>

<p>I can then delete SECRET_KEY.TXT, inputfile.py, and rename outputfile.py into inputfile.py
Most likely not the most efficent way, but it works...</p>

<p>EXCEPT for one thing! I have a lot a special characters in this inputfile, that's why I used !secret_key! for example...</p>

<p>But I also have a lot of:</p>

<pre><code>[some stuff]
</code></pre>

<p>and in the outputfile, it appears as:</p>

<pre><code>[some stuff
</code></pre>

<p>So I guess I'm close, but I can't find a delimiter which will allow every single character to be outputed!</p>