title: Padding filenames with 0s in batch
body: <p>I'm trying to edit some filenames with padded 0s from the command line. I want T9 to become T009 for example.</p>
  
  <p>I tried:  </p>
  
  <pre><code>@echo off
  set num=99
  set newnum=000%num%
  set newnum=%newnum:~-4%
  rename "T%num%.bmp" "T%newnum%.bmp"
  pause
  </code></pre>
  
  <p>which did indeed changed file t99.bmp to T0099.bmp as I expected. However, when I try a loop like: </p>
  
  <pre><code>@echo off
  FOR /L %%G IN (1,1,100) DO (
      set num=%%G
      echo %%G
      set newnum=0000%num%
      set newnum=%newnum:~-4%
      echo %newnum%
      ren "T%num%.bmp" "T%newnum%.bmp"
  )
  pause
  </code></pre>
  
  <p>It tells me that 'ECHO is off' and 'The system cannot find the file specified.' Note, it DOES echo %%G so I have no clue what's going on. I think it has something to do with being inside the loop? But I haven't been able to figure that out by googling.</p>
  
  <p>My overall question is: Is there an obvious mistake in my methodology? and Why does the first chunk of code behave as I expect, but not the second?</p>
  
  <p>I just learned batch like an hour ago and so far it's given me a headache. haha.</p>