Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I generally prefer to not include enclosing quotes in my variable values. I then add the enclosing quotes as needed at expansion time. Instead of <code>set var="value"</code>, I like to use <code>set "var=value"</code>. If I need enclosing quotes later on then I use <code>"%var%"</code>.</p> <p><em>Note: none of my code below has been tested. There could be some silly bugs, but I am confident the overall concepts are sound</em></p> <p>The following simple script should handle most situations. The <code>/M</code> option is used so that FINDSTR does not waste its time searching every line for multiple - it exits as soon as it finds a single line that matches.</p> <pre><code>@echo off setlocal set "SEARCHSTRING=search for this text" set "STARTPATH=C:\StartFolder" set "MATCHFILES=*.txt" set "APPENDSTRING=Appended text" for /r "%STARTPATH%" %%F in ("%MATCHFILES%") do ( &gt;nul findstr /mbic:"%SEARCHSTRING%" "%%~F" || &gt;&gt;"%%F" echo(%APPENDSTRING% ) </code></pre> <p>But there are many scenarios that could break the above code.</p> <ul> <li><p>Poison characters like unquoted <code>&amp;</code>, <code>|</code> etc. could cause problems when expanding <code>%APPENDSTR%</code></p></li> <li><p>Poison characters could cause problems when expanding <code>"%SEARCHSTRING%"</code> if SEARCHSTRING also contains quotes.</p></li> <li><p>FINDSTR has awkward escape rules for <code>\</code> and <code>"</code> characters within the literal search string.</p></li> <li><p>FINDSTR may not work properly if the SEARCHSTRING has certain byte codes > 128.</p></li> </ul> <p>See <a href="https://stackoverflow.com/q/8844868/1012053">What are the undocumented features and limitations of the Windows FINDSTR command?</a> for more info about complexities of using FINDSTR</p> <p>The easiest way to make the code work in all situations is to use delayed expansion to avoid problems with poison characters in variable values, and to store the search string in a temp file and use the FINDSTR <code>/G</code> option. Any <code>\</code> character within the search string must be escaped as <code>\\</code>. Delayed expansion must be disabled within the FOR loop so that file names that contain <code>!</code> are not corrupted.</p> <pre><code>@echo off setlocal disableDelayedExpansion set "SEARCHSTRING=search for this text" set "STARTPATH=C:\StartFolder" set "MATCHFILES=*.txt" set "APPENDSTRING=Appended text" set "tempFile=%temp%\search%random%_" setlocal enableDelayedExpansion &gt;"%tempFile%search.txt" echo(!SEARCHSTRING:\=\\! &gt;"%tempFile%append.txt" echo(!APPENDSTRING! endlocal for /r "%STARTPATH%" %%F in ("%MATCHFILES%") do ( &gt;nul findstr /mbig:"%tempFile%search.txt" "%%~F" || type "%tempFile%append.txt" &gt;&gt;"%%~F" ) del "%tempFile%*.txt" </code></pre>
 

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