Note that there are some explanatory texts on larger screens.

plurals
  1. POCMD/BATCH Programming - File content search and file content amendment
    text
    copied!<p>I have a requirement to generate a simple .BAT script/file. I assumed it to be easy but how wrong I was :-(</p> <p>The BAT script has to (I believe):</p> <ol> <li>Take a const searchstring to search for</li> <li>Take an absolute folder path to a "start" directory</li> <li>Take a filename mask/regular expression to search for </li> <li>Take a const string to be "appended" to the content of the file</li> <li>Recursively iterate through all files in the start directory and subdirectories (n level down) searching (and "processing") any files that match the filename mask/regular expression</li> <li>Search the content of the "current" file (all "are text" files that can be opened in NOTEPAD), for the searchstring. The searchstring may be found 0, 1 or many times anywhere in the file.</li> <li>If searchstring is found, then do nothing. If the searchstring is not found then append the new string to the end of the file. Voila! (in theory)</li> </ol> <p>Heres what I have already, not really working. I have several problems that I can see. How to get the "current file" and possibly also how to "reset" the ERRORLEVEL after each file.</p> <pre><code>SET SEARCHSTRING="search for this text" SET STARTPATH="C:\StartFolder\*" SET MATCHFILES="*.txt" SET APPENDSTRING="Appended text" findstr /c:%SEARCHSTRING% /b /i /d:%STARTPATH% %MATCHFILES% SET FOUND=%ERRORLEVEL% REM 0=true, 1=false IF %FOUND%==1 ( REM ISSUE: How to get "current" file path??? REM %APPENDSTRING% &gt;&gt; %CURRENTFILEPATH% ) </code></pre> <p>Any questions/suggestions more than welcome</p> <p>Cheers</p> <p>kyle </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