title: DOS Batch Rename Files
body: <p>I have a large set files with names structured <code>string_int_int_int_string.extension</code>, and would like to batch rename them with left zero padding to 7 digits on the second int. </p>
  
  <p>Example: rename <code>stringA_1_2_3_stringB.jpg</code> to <code>stringA_1_0000002_3_stringB.jpg</code>.</p>
  
  <p>I've seen some helpful posts <a href="https://superuser.com/questions/403183/how-to-rename-multiple-files-in-multiple-folders-with-1-command/403185#403185">here</a>, <a href="https://stackoverflow.com/questions/1025470/batch-rename-file-extensions-including-subdirectories">here</a>, and <a href="https://stackoverflow.com/questions/5417979/batch-rename-sequential-files-by-padding-with-zeroes">here</a> but haven't quite done it.</p>
  
  <p>Here is what I have so far (not working, of course):</p>
  
  <pre><code>dir /b *.* &gt;temp.txt
  
  for /f "tokens=%%1,%%2,%%3,%%4,%%5 delims=_" %x in (temp.txt) do (
      setlocal enabledelayedexpansion
      set PAD=000000%%k
      set PAD=!PAD:~7!
      ren "%%i_%%j_%%k_%%l_%%m" %%i_%%j_%PAD%_%%l_%%m
  )
  </code></pre>
  
  <p>I specifically want to do this with a batch file, not some other language or tool. (I'm aware of the various renaming tools out there.)</p>
  
  <p>Any help is most welcome!</p>