Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Update</h2> <p>I have added a new script which will replace the search term with the desired code right into the line. The script can handle special characters.</p> <hr> <h2>Limitations</h2> <ol> <li>Leading Close Bracket <strong><code>]</code></strong> characters will be trimmed from the beginning of lines. Not an issue since there should be no lines in HTML beginning with this character. <strong>(This can be fixed if needed)</strong></li> <li>The percent sign <strong><code>%</code></strong> character cannot be used in either the search term or replacement term.</li> </ol> <h2>Notes</h2> <ol> <li>Lines cannot contain an odd number of double quotations <strong><code>"</code></strong> so I double the double quotations <strong><code>""</code></strong> to ensure an even number. This means that if you have quotations in either of the strings, they must as well be doubled!</li> <li><p>To use the script, just replace the <strong>search term</strong> and <strong>replacement term</strong> with what you want on the following line of code.</p> <p><code>set "_=%_:</code><strong>search term</strong><code>=</code><strong>replacement term</strong><code>%"</code></p></li> </ol> <h2>New Script.bat</h2> <pre><code>@echo off setlocal EnableExtensions DisableDelayedExpansion :: Limitations :: 1. Any leading close bracket ] characters will be trimmed due to delims=]. for /r %%F in (html.txt) do if exist "%%~fF" ( for /f "tokens=1,* delims=]" %%K in ('type "%%~fF" ^| find /n /v ""') do ( set "_=%%L" call :Expand_ ) ) goto End :Expand_ :: NULL is a blank line or line with only a close bracket ]. if not defined _ echo. &amp; goto :eof :: Ensure even number of double quotation marks. set "_=%_:"=""%" :: Inject the code. set "_=%_:&lt;/body&gt;=&lt;code&gt;To Inject&lt;/code&gt;&lt;/body&gt;%" :: Escape batch special characters. set "_=%_:^=^^%" set "_=%_:&lt;=^&lt;%" set "_=%_:&gt;=^&gt;%" set "_=%_:&amp;=^&amp;%" set "_=%_:|=^|%" :: Revert quotations. set "_=%_:""="%" :: Display echo(%_% goto :eof :End endlocal pause &gt;nul </code></pre> <hr> <h2>Original</h2> <p>This should do what you want. No Delayed Expansion needed. Should support all special characters.</p> <h2>Limitations</h2> <ol> <li>Leading Close Bracket <strong><code>]</code></strong> characters will be trimmed. Not an issue since there should be no lines in HTML beginning with the close bracket character. <strong>(This can be fixed if needed.)</strong></li> <li>The percent sign <strong><code>%</code></strong> character cannot be used in either the search term or replacement term.</li> </ol> <h2>Notes</h2> <ol> <li>Lines cannot contain an odd number of double quotations <strong><code>"</code></strong> so I double the double quotations <strong><code>""</code></strong> to ensure an even number. This means that if you have quotations in the string to match, they must as well be doubled. <strong>(Does not apply to your scenario)</strong></li> <li>Delayed Expansion cannot be used around this line <code>for /f %%S in ('echo "%xLine%"^| find /i "&lt;/body&gt;"') do (</code> else <code>!</code> exclamation marks will cause an issue.</li> </ol> <h2>Script.bat</h2> <pre><code>@echo off setlocal EnableExtensions for /r %%F in (*.mht) do if exist "%%~fF" ( rem Limitation - Any leading close bracket ] characters will be trimmed. for /f "tokens=1,* delims=]" %%K in ('type "%%~fF" ^| find /n /v ""') do ( set "xLine="%%L"" call :Match echo(%%L&gt;&gt;"%%~dpF\new_%%~nF%%~xF" ) rem del "%%~fF" rem ren "%%~dpF\new_%%~nF%%~xF" "%%~nxF" ) goto End :Match setlocal EnableExtensions DisableDelayedExpansion rem Double the double quotations to ensure even number of double quotations. set "xLine=%xLine:"=""%" for /f %%S in ('echo "%xLine%"^| find /i "&lt;/body&gt;"') do ( rem Add your code to inject here. Copy the template echo below. rem Note that special characters need to be escaped. echo Inject Code&gt;&gt;"%%~dpF\new_%%~nF%%~xF" ) endlocal goto :eof :End endlocal pause &gt;nul </code></pre> <p>This will output the new file to <code>new_&lt;filename&gt;.mht</code> If you want to replace the old file with the new file, just remove the <code>rem</code> command from before the <code>del</code> and <code>ren</code> commands.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload