I have a requirement to generate a simple .BAT script/file. I assumed it to be easy but how wrong I was :-(
The BAT script has to (I believe):
- Take a const searchstring to search for
- Take an absolute folder path to a "start" directory
- Take a filename mask/regular expression to search for
- Take a const string to be "appended" to the content of the file
- 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
- 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.
- 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)
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.
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% >> %CURRENTFILEPATH%
)
Any questions/suggestions more than welcome
Cheers
kyle