title: String replacement in loops and recursions with unexpected side effects
body: <p>I'm trying to create a program where I go through files (in a folder and all its sub folders) and dynamically replace parts of a line with other parts. To do this I made a program but found out that it behaved unexpectedly.</p>
  
  <p>Instead of just the string being replaced the WHOLE line got replaced. Then I did some experiments and saw that outside of the loops the string replacement behaved as expected but inside.......strangeness began. </p>
  
  <p>Below is a pretty basic example with a normal string replacement inside a loop.</p>
  
  <pre><code>@echo off   
  call :Rekursiv
  goto :eof
  
  :Rekursiv
      set str=teh cat in teh hat
      echo "Outside"
      echo %str%
      set str=%str:teh=the%
      echo %str%  
      echo "Outside end"
  
      for /D %%d in (*) do (
              cd %%~fd
  
              set str=teh cat in teh hat
              echo %str%
              set str=%str:teh=the%
              echo %str%      
  
              call :Rekursiv      
  
              cd..
      )
  
      exit /b     
  </code></pre>
  
  <p>Now normally I would expect:</p>
  
  <pre><code>teh cat in teh hat
  the cat in the hat
  </code></pre>
  
  <p>and a few outside and outside end strewn in between, but what I get is:</p>
  
  <pre><code>"Outside"
  teh cat in teh hat        &lt;--- as expected
  the cat in the hat        &lt;--- as expected
  "Outside end"
  the cat in the hat        &lt;--- Completely unexpected as its the replaced string already!
  the cat in the hat
  "Outside"
  teh cat in teh hat
  the cat in the hat
  "Outside end"
  the cat in the hat
  the cat in the hat
  "Outside"
  teh cat in teh hat
  the cat in the hat
  "Outside end"
  the cat in the hat
  the cat in the hat
  "Outside"
  teh cat in teh hat
  the cat in the hat
  "Outside end"
  </code></pre>
  
  <p>All replacements outside the loops work as I thought they should. But inside the loop it looks like the sets are all done BEFORE the echoes are done. As I think this and my line replacement problem have the same source/cause my question is here: WHY does the program produce this unexpected output? And what can I do to get the expected output?</p>
  
  <p>EDIT: After MCDNs comment I tried things out:</p>
  
  <pre><code>@echo off   
  call :Rekursiv
  goto :eof
  
  :Rekursiv
      set str=teh cat in teh hat
      echo "Outside"
      echo %str%
      set str=%str:teh=the%
      echo %str%  
      echo "Outside end"
  
  
  set "test=before"
  if defined test (
      set "test=after"
      echo %test%
  )   
  
      for /D %%d in (*) do (
              cd %%~fd
  
              set str=teh cat in teh hat
              echo %str%
              set str=%str:teh=the%
              echo %str%      
  
  set "test=before"
  if defined test (
      set "test=after"
      echo %test%
  )
  
              call :Rekursiv      
  
              cd..
      )
  </code></pre>
  
  <p>The first occurance of "echo %test%" is "before"  the second is "after"</p>