title: Batch files: List all files in a directory with relative paths
body: <p>Concerning Windows batch files: Is there a way to list all the files (or all of a specific type) in a certain directory and its subdirectories, including the paths relative to the current (or the search) directory in the list?</p>
  
  <p>For example, if I want all the .txt files in the current directory and subdirectories with their full paths, I can do</p>
  
  <pre><code>for /r . %%g in (*.txt) do echo %%g &gt;&gt; C:\temp\test.txt
  </code></pre>
  
  <p>or</p>
  
  <pre><code>dir *.txt /b /s &gt;&gt; C:\temp\test.txt
  </code></pre>
  
  <p>and I will get something like</p>
  
  <pre><code>C:\test\Doc1.txt
  C:\test\subdir\Doc2.txt
  C:\test\subdir\Doc3.txt
  </code></pre>
  
  <p>If I do</p>
  
  <pre><code>for /r . %%g in (*.txt) do echo %%~nxg &gt;&gt; C:\temp\test.txt
  </code></pre>
  
  <p>I will get something like</p>
  
  <pre><code>Doc1.txt
  Doc2.txt
  Doc3.txt
  </code></pre>
  
  <p>But what I really want is:</p>
  
  <pre><code>Doc1.txt
  subdir\Doc2.txt
  subdir\Doc3.txt
  </code></pre>
  
  <p>Is it possible?</p>
  
  <p>If my post is too confusing: I basically want <em><a href="https://stackoverflow.com/questions/245698/list-files-recursively-in-linux-with-path-relative-to-the-current-directory">List files recursively in Linux CLI with path relative to the current directory</a></em>, but just for Windows.</p>