title: How to find and replace all files in a folder using a dynamic value
body: <p>I'm having a few html files where I need to add the pagename as a tag value. Plenty of tools to do mass find and replace, but than you are restricted to a static text, while I need to add the actual filename.</p>
  
  <p>It works perfectly fine on a single file, but if I start to loop into a directory it goes wrong, and I'm not able to figure out where things go wrong. </p>
  
  <p>Doing this on a single predefined file works as expected :</p>
  
  <pre class="lang-batch prettyprint-override"><code>@echo off &amp; setLocal enableDELAYedexpansion
  rem Create tmp file to store modified information
  set tmpFile=%CD%\test\tmp.txt
  set myFile=%CD%\test\test.html
  set page=test
  
  rem delete current tmp file
  if exist "%tmpFile%" del /f /q "%tmpFile%"
  
  rem find and replace values
  set search="container"
  set replace="container_!page!"
  
  rem loop through all the lines and replace the string in question
  for /F "tokens=*" %%a in (%myFile%) do (
  set myLines=%%a
  set myLines=!myLines:%search%=%replace%!
  echo !myLines!  &gt;&gt; %tmpFile%
  )
  rem update current file with updated content
  type !tmpFile! &gt; !myFile!
  )
  pause
  </code></pre>
  
  <p>The above nicely finds my string, and replaces it with the one I want (though still static in this case)</p>
  
  <p>But if I want the same logic to loop through more files it goes wrong. This is the code I use :</p>
  
  <pre class="lang-batch prettyprint-override"><code>@echo off &amp; setLocal enableDELAYedexpansion
  rem Create tmp file to store modified information
  set tmpFile=%CD%\test\tmp.txt
  rem switch to test folder
  cd %CD%/test
  
  rem Start the loop
  For /R  %%F in (*.html) do (
  
  rem get page name
  set page=%%~nxF
  set MyFile=%%F
  rem delete current tmp file
  if exist "%tmpFile%" del /f /q "%tmpFile%"
  
  rem find and replace values
  set search="container"
  set replace="container_!page!"
  
  rem loop through all the lines and replace the string in question
  for /F "tokens=*" %%a in (%%F) do (
  set myLines=%%a
  set myLines=!myLines:%search%=%replace%!
  echo !myLines!  &gt;&gt; %tmpFile%
  )
  
  rem update current file with updated content
  type !tmpFile! &gt; !myFile!
  
  )
  
  pause
  </code></pre>
  
  <p>This basically doesn't do anything, I've been trying some otehr variations but these result in either empty pages or pages that contain myLines= as a literal string.</p>
  
  <p>What am I overlooking here ?</p>
  
  <p>[EDITED AREA]</p>
  
  <p>I've managed to get it working partially, but have strange unwanted side effects.</p>
  
  <p>Not using search / replace parameters but using it directly in the replace parts as follows works :</p>
  
  <pre><code>for /F "tokens=*" %%a in (%%F) do (
  set myLines=%%a
  set "myLines=!myLines:container=container_%%~nF!"
  echo !myLines!  &gt;&gt; %tmpFile%
  )
  </code></pre>
  
  <p>This replaces container with container_pagename as I wanted , BUT it also removes all esclamation marks on my htm, which is quite annoying, as it ruins all the comments on the page.</p>
  
  <pre><code>so &lt;!-- comment --&gt;
  becomes &lt;-- comment --&gt;
  </code></pre>
  
  <p>Any clue where it goes wrong, or why using parameters doesn't work for me ?</p>