|i went back and looked at my earlier script, and decided it wasn't quite right for the job, so I reworked the whole theme|
(involving a lot more effort than i first imagined due to attempting to preserve the exclamations)
EDITED: renamed file "temp" to be "tempfile",
located in any convenient directory - I used a made-up "\work\" to represent such a directory.
EDIT AGAIN! might have gone a tad overboard with the debugging, but better too much than not enough! ;-)
@echo off & setlocal disabledelayedexpansion
for /f "tokens=*" %%a in ('dir /b /s /a-d *.xml') do (
:: first, get the parent directoryname
:: finish getting the parentname
for /f "tokens=*" %%b in ("%xx%") do set parent=%%~nxb& set fil="%%a"
echo file: %fil%, parent directory: %xx%
:: build list of the linenumbers of lines that have the target-string
for /f "tokens=1 delims=" %%b in ('find /n /i "user.mode.production" ^< %fil%') do set line%%b=%%b
:: now, process the file itself. Use any convenient existing directory to hold "tempfile"
echo clearing workfile:
echo off > \work\tempfile
echo \work\tempfile has been cleared for the above file. it should be empty at this point...
:: "find" is used to preserve the format of blank lines and leading spaces etc.
echo file %fil% content being iterated:
for /f "tokens=1* delims=" %%b in ('find /v /n ""^<"%fil%"') do call :replecho
::foll line disabled for safety-check. enable when ready for live operation
echo MOVE command content: move /y \work\tempfile %fil%
::move /y \work\tempfile %fil%
set /a cc+=1
if not defined line%cc% (
:: a "regular" line
for %%d in (1) do >> \work\tempfile echo.%%c & echo.%%c
) else (
:: a "target" line, do the insertion
echo injection of %parent% at line %cc%
for %%d in (1) do set zz="%%c"
>> \work\tempfile echo !zz:~1,-1!
note that if any of your target lines have exclms, the script will remove them.
Had i known how this script would kick my ass, i would have used vbscript (much easier!)