title: Renaming a list of files #1 by another list of files #2
body: <p>Renaming a list of files (in one folder) contained in a text file like <code>MyList1.txt</code> by another list of files contained in another text file like <code>MyList2.txt</code>.</p>
  
  <p>I would like to use a Windows command batch file (not PowerShell, script, etc.) which renames a list of files in one folder, contained in a text file, by another list of files contained in another text file.</p>
  
  <p>Suppose I have a list of files inside a text file. These files are in one folder, for example <code>D:\Librarian</code></p>
  
  <p>File <strong>D:\Test\MyList1.txt</strong> contains:</p>
  
  <pre><code>Directory.zip
  Subject.zip
  Penny.zip
  Car.zip
  Auto.zip
  One_Upon_A_time.zip
  All is relative.zip
  Zen of graphics programming – Corilolis.zip
  PC Magazine – July 1999 No.22.zip
  Bytes is over with quantum.zip
  </code></pre>
  
  <p>And I want to replace them by the file names listed in <strong>D:\Test\MyList2.txt</strong> containing:</p>
  
  <pre><code>Patatoes.zip
  Chips.zip
  Hot Dogs Sweet.zip
  Ice Cream_Very – Good.zip
  We must eat to live.zip
  Ketchup on potatoes is a muxt.zip
  The Magazine are owned by few guys.zip
  Desert are not necessary_in life.zip
  Sugar_is_a_legal_drug.zip
  People-who_don’t-like_pizzas_don’t like bread.zip
  </code></pre>
  
  <p><strong>So</strong></p>
  
  <p><code>Directory.zip</code> will become <code>Patatoes.zip</code><br>
  <code>Subject.zip</code> will become <code>Chips.zip</code><br>
  <code>Penny.zip</code> will become <code>Hot Dogs Sweet.zip</code><br>
   etc.</p>
  
  <p>Both <code>MyList1.txt</code> and <code>MyList2.txt</code> have same number of lines (same number of file names).</p>
  
  <p><strong>OS is:</strong> Windows 10.<br>
  Must use batch (cmd)</p>
  
  <p>One folder - <code>D:\Librarian</code> have files to be renamed.<br>
  Another folder have the two files <code>MyList1.txt</code> and <code>MyList2.txt</code></p>
  
  <p><code>MyList1.txt</code> contains the list of files from <code>D:\Librarian</code><br>
  <code>MyList2.txt</code> contains the new name of files for <code>D:\Librarian</code></p>
  
  <p>------<strong>Begin 1: dbenham</strong>----------------------------------------------------------</p>
  
  <p>I have tried what you wrote and it's working except for 2 file names.<br>
  Probably Windows command interpreter doesn't accept some punctuation in these file names.</p>
  
  <p><strong>Here what appear on command prompt (DOS under Windows 10):</strong><br>
  (I remove <code>@echo off</code> of course to be able to see that.)</p>
  
  <pre><code>D:\&gt;setlocal disableDelayedExpansion
  D:\&gt;pushd "d:\Librarian"
  d:\Librarian&gt;(for /F "usebackq eol=: delims=" %F in ("d:\Test\MyList1.txt") do (
  set "from=%F"
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  ) ) 0 (&lt;)  d:\Test\MyList2.txt
  </code></pre>
  
  <p>It's not (&lt;) but only &lt; but for x reason 0&lt; is not visible.</p>
  
  <pre><code>d:\Librarian&gt;(
  set "from=Directory.zip "
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  )
  
  d:\Librarian&gt;(
  set "from=Subject.zip"
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  )
  </code></pre>
  
  <p><strong>...</strong></p>
  
  <pre><code>d:\Librarian&gt;(
  set "from=Zen of graphics programming  Corilolis.zip"
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  )
  </code></pre>
  
  <p><strong>The system cannot find the file specified.</strong></p>
  
  <pre><code>d:\Librarian&gt;(
  set "from=PC Magazine  July 1999 No.22.zip""
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  )
  </code></pre>
  
  <p><strong>The system cannot find the file specified.</strong></p>
  
  <pre><code>d:\Librarian&gt;(
  set "from=Bytes is over with quantum.zip"
   setlocal enableDelayedExpansion
   set "to="
   set /p "to="
   if defined to ren "!from!" "!to!"
   endlocal
  )
  </code></pre>
  
  <p>Windows command interpreter doesn't like <code>-</code> character probably in the following file names:</p>
  
  <pre><code>PC Magazine - July 1999 No.22.zip
  Zen of graphics programming - Corilolis.zip
  </code></pre>
  
  <p>But since a lot of my files have the hyphen character, I would like an advice to get the batch file working also for such file names.</p>
  
  <hr>
  
  <p>Later... After to use CMD to rename all files (inside <code>MyList1.txt</code> and <code>MyList2.txt</code> and in the folder <code>D:\Librarian</code>) all work good.</p>
  
  <p>I think I copy and paste files from this page and so not same characters for all even if <code>-</code> can appear to be <code>-</code> like you say they can be different on console or on web page.</p>
  
  <p>I can't say I understand all what you did (so bright)!</p>
  
  <p>For example why do:</p>
  
  <pre><code>pushd "d:\Librarian"&lt;br&gt;
  (&lt;)d:\Test\MyList2.txt
  (No () around the &lt;)
  </code></pre>
  
  <p>I find Windows commands are just extraordinary. Well more to see what people, like you, can do so with so few commands. Just unbelievable!</p>
  
  <p>------<strong>End 1: dbenham</strong>----------------------------------------------------------</p>
  
  <p>Hope to not bore you ...</p>
  
  <p>Can I do the same if my files are in folder and subfolders?</p>
  
  <p>Suppose my files are distributed in the following folders:</p>
  
  <pre><code>D:\Librarian\
  D:\Librarian\S1\
  D:\Librarian\Plane\
  </code></pre>
  
  <hr>
  
  <p><strong>MyList1.txt</strong></p>
  
  <pre><code>D:\Librarian\Directory.zip
  D:\Librarian\Subject.zip
  D:\Librarian\Penny.zip
  
  D:\Librarian\S1\Car.zip
  D:\Librarian\S1\Auto.zip
  D:\Librarian\S1\One_Upon_A_time.zip
  D:\Librarian\S1\All is relative.zip
  
  D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip
  D:\Librarian\Plane\PC Magazine - July 1999 No.22.zip
  D:\Librarian\Plane\Bytes is over with quantum.zip
  </code></pre>
  
  <hr>
  
  <p><strong>MyList2.txt</strong></p>
  
  <pre><code>D:\Librarian\Patatoes.zip
  D:\Librarian\Chips.zip
  D:\Librarian\Hot Dogs Sweet.zip
  
  D:\Librarian\S1\Ice Cream_Very - Good.zip
  D:\Librarian\S1\We must eat to live.zip
  D:\Librarian\S1\Ketchup on potatoes is a must.zip
  D:\Librarian\S1\The Magazine are owned by few guys.zip
  
  D:\Librarian\Plane\Desert are not necessary_in life.zip
  D:\Librarian\Plane\Sugar_is_a_legal_drug.zip
  D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip
  </code></pre>
  
  <p>What I would like is for example:</p>
  
  <pre><code>ren "D:\Librarian\Directory.zip" "D:\Librarian\Patatoes.zip"
  ren "D:\Librarian\Plane\Zen of graphics programming - Corilolis.zip" "D:\Librarian\Plane\People-who_don't-like_pizzas_don't like bread.zip"
  </code></pre>