title: Recursively append folder name to the files in Windows batch file
body: <p>I would like to append my folder name to all the available .txt files inside a subfolder. Below is the file/directory structure. I need to achieve this in Windows BATCH script.</p>
  
  <blockquote>
    <p>C:\Source\Source1\1\a.txt C:\Source\Source1\1\b.txt</p>
    
    <p>C:\Source\Source1\2\a.txt C:\Source\Source1\2\b.txt</p>
    
    <p>C:\Source\Source2\3\a.txt C:\Source\Source2\3\b.txt</p>
  </blockquote>
  
  <p>The above files should be renamed like below:</p>
  
  <blockquote>
    <p>C:\Source\Source1\1\1_a.txt C:\Source\Source1\1\1_b.txt</p>
    
    <p>C:\Source\Source1\2\2_a.txt C:\Source\Source1\2\2_b.txt</p>
    
    <p>C:\Source\Source2\3\3_a.txt C:\Source\Source2\3\3_b.txt</p>
  </blockquote>
  
  <p>Similary, I have Source1...Source30 and under each source directory, I will have multiple folders with different numbers. I need to rename all the files under these directories and append the number(directory name) to the file name.</p>
  
  <p>So far below is what I wrote:</p>
  
  <pre><code>for %%* in (.) do set CurrDirName=%%~nx*
  echo %CurrDirName%
  
  for /r %%x in (*.txt) do ren "%%x" "%CurrDirName%_%%x"
  </code></pre>
  
  <p>With this, I am able to achieve it in a single directory. I couldn't make it recursive. Could you guys please help me with this.</p>