title: Directory mirror and conditional renaming
body: <p>I have a directory of active text files which are used by machines on a manufacturing floor. These files are revision controlled by using an appended tag (eg. 000000<strong>_R1</strong>.pim). These machines can only receive a file with a 6 digit string name (000000.pim). Normally we handle this by using a simple FTP program and renaming them individually during transfer (FTP is the only option with these old machines, unfortunately). </p>
  
  <p>I would like to create a mirror directory that holds the files already renamed, so that I don't have to rename every time programs are pushed to the machine's hard drive. This allows me to provide hard evidence of revision control for the customers.</p>
  
  <p>I have no IT privileges, and no ability to even install software on my computer.</p>
  
  <p>I've written a batch file to do this for me, but it takes about a minute to complete, which is rather tedious. The script tends to delete, copy, and rename around 500 files every time it's run (out of 2,700 files).</p>
  
  <pre><code>@echo off 
  echo **************************************************** 
  echo Please wait while the Manufacturing Directory is updated 
  echo **************************************************** 
  Set TransTXT="%USERPROFILE%\Desktop\pim_Transfer.txt"
  Set LogTXT="%USERPROFILE%\Desktop\Transfer_Log.txt"
  set REMOTEHD="M:\DMS_8055"
  set FILELOCA="T:\Fagor\PRG"
  REM *** MOVE ALL FILES TO MANUFACTURING NETWORK
  robocopy %FILELOCA% %REMOTEHD% /LOG:%LOGTXT% /V /NP /NS /NDL /NJH /NJS /PURGE
  echo ****************************************************
  echo Copy Complete
  echo ****************************************************
  REM RENAME ALL FILES
  echo Exchanged %DATE% %TIME% &gt; %transtxt%
  setlocal EnableDelayedExpansion
  echo ****************************************************
  ECHO RENAMING FILES TO MATCH MACHINE REQUIREMENTS:
  echo ****************************************************
  cd /d "%REMOTEHD%"
  for %%x in ("%REMOTEHD%\*.pim") do (
      set curn=%%~nx
      set redn=!curn:~0,6!
      IF /I !curn! neq !redn! (
          echo "!curn!" &gt;&gt; %transtxt%
          IF EXIST "!redn!.pim" (
              echo "!curn! REPLACED !redn!" &gt;&gt; %transtxt%
              del "!redn!.pim"
              ren "%%x" "!curn:~0,6!.pim"
          ) ELSE (
              ren "%%x" "!curn:~0,6!.pim"
          )
      ) 
  )
  REM *** copy program to a temporary folder
  echo ****************************************************
  echo Rename Complete
  echo ****************************************************
  REM start notepad %LogTXT%
  REM start notepad %transtxt%
  </code></pre>
  
  <p>I'm wondering if there is a more elegant way to handle this - possibly something that doesn't require so many read/writes each time it's ran - but it also needs to be robust enough that it doesn't miss any changes to the source directory. There is a chance that very old files could be moved into my source directory due to customer demands, so using the timestamps won't necessarily work.</p>