title: How to rename files in a batch loop
body: <p>I am currently writing a script for compressing mp3 songs in a directory. It looks like this.</p>
  
  <pre><code>SET BITRATE=128k
  SET EXTENSION=mp3
  SET MP3_CODEC=libmp3lame
  
  for %%f in (.\*.%EXTENSION%) do (
      ffmpeg -y -i "%%f" -acodec %MP3_CODEC% -ab %BITRATE% -id3v2_version 3 "%%f.%EXTENSION%"
      del "%%f"
      ren "%%f.%EXTENSION%" "%%f"
  )
  </code></pre>
  
  <p>Which works fine on compressing and deleting the original file. But at the time of <strong>renaming the new file</strong> I get a <code>The syntax of the command is incorrect</code> error.</p>
  
  <p>Isolated, the failing code is this</p>
  
  <pre><code>for %%f in (.\*.mp3) do ren "%%f" "%%f.mp3"
  </code></pre>
  
  <p>I know the renaming workaround seems bad but the <code>%%f</code> variable stores the folder on it (i.e. ".\song.mp3") so I cannot change the name of the file, just its extension (which must be the same for ffmpeg to work).</p>
  
  <h2>Repeating the error</h2>
  
  <p>Having installed ffmpeg in Windows, place a X.mp3 file and this script in a folder and run the script. It will compress the file onto a new file called "X.mp3.mp3", deleted the original "X.mp3" file and fail at the time of renaming "X.mp3.mp3" to "X.mp3".</p>
  
  <p>If I run those commands manually in a console it does work.</p>