title: Batch Syntax For Loop If Exists
body: <p>Trying to create a batch to convert .WTV files to mpg files. The code below I want to run once a night to convert the daily recordings for use on my media server. The section I am concerned about syntax is the for loop line. I only want to run the script on the files that have not been converted and moved. I cant delete the original in case and error occurs. The last move loop is to get all the files it couldn't move on the first try.</p>
  
  <p>the input files look like : show.wtv</p>
  
  <p>the output in the file is show.wtv.mpg (which I'm ok with)</p>
  
  <p>I am not sure how to write that for loop to get the desired functionality. Any help is appreciated.</p>
  
  <p>Original idea came from <a href="http://ireckon.net/2009/10/converting-wtv-to-mpg-in-windows-7/" rel="nofollow">http://ireckon.net/2009/10/converting-wtv-to-mpg-in-windows-7/</a></p>
  
  <pre><code>@echo off
  
  set recordedtv="D:\Recorded TV\"
  set destfolder="D:\Videos\Recorded TV\"
  set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
  set wtvconv="C:\Windows\ehome\WTVConverter.exe"
  
  for %%f in (%recordedtv%*.wtv) Do If Not Exist "%destfolder%%%f.mpg" (
  %wtvconv% "%%f" "%%f.dvr-ms"
  %ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
  del "%%f.dvr-ms"
  move "%%f.mpg" %destfolder%
  )
  
  for %%f in (%recordedtv%*.mpg) Do move "%%f" %destfolder%
  </code></pre>
  
  <p>UPDATE: Ended up using this</p>
  
  <pre><code>@echo off
  
  set "recordedtv=D:\Recorded TV\"
  set "destfolder=D:\Videos\Recorded TV\"
  set ffmpeg="D:\TV Converter\FFMPEG\ffmpeg.exe"
  set wtvconv="C:\Windows\ehome\WTVConverter.exe"
  
  for %%f in ("%recordedtv%*.wtv") do If Not Exist "%destfolder%%%~nxf.mpg" (
  %wtvconv% "%%f" "%%f.dvr-ms"
  %ffmpeg% -y -i "%%f.dvr-ms" -vcodec copy -acodec copy -f dvd "%%f.mpg"
  del "%%f.dvr-ms"
  move "%%f.mpg" "%destfolder%"
  )
  
  for %%f in ("%recordedtv%*.mpg") Do move "%%f" "%destfolder%"
  </code></pre>