title: How to rename PNG files in different sub directories with multiple condition using batch script?
body: <p>I have a work folder. In that folder there are multiple sub folders. Each subfolder has 100 png files.</p>
  <p>The 100 png files have a name which includes X and Y coordinates, which represent a position in a 10x10 grid.</p>
  <p>Each png has a name format like this [ArbitraryFileName]<em>[xCoordinate]</em>[yCoordinate].png. The coordinates range from 0 to 9. Examples:</p>
  <pre><code>.../Root Folder/Layer 0/layer0_x00_y00.png
  .../Root Folder/Layer 0/layer0_x01_y09.png
  .../Root Folder/ArbitraryFolderName/AbitraryFileName_x01_y09.png
  .../Root Folder/Layer 14/layer14_x00_y00.png
  .../Root Folder/Layer 43/layer43_x00_y00.png
  </code></pre>
  <p>All I need the script to do is rename the files and reverse the order of the Y coordinate for each set.</p>
  <pre><code>layer0_x00_y00.png would be renamed to layer0_x00_y09.png
  layer0_x00_y01.png would be renamed to layer0_x00_y08.png
  layer0_x00_y02.png would be renamed to layer0_x00_y07.png
  layer0_x00_y03.png would be renamed to layer0_x00_y06.png
  layer10_x09_y09.png would be renamed to layer10_x09_y00.png
  RandomFile_x00_y00.png would be renamed to RandomFile_x00_y09.png
  </code></pre>
  <p>and so on.</p>
  <p>All I need to do is</p>
  <ul>
  <li>Run the script file in the root folder</li>
  <li>Have it rename and reverse the order of the Y coordinate for all of the images in all of the subfolders within the root folder.</li>
  </ul>
  <p>I've very basic understanding in batch script, so I put together following code for my need but it doesn't iterate through sub folders and it doesn't work when it is used once again. What am I doing wrong? Can some one help me on this?</p>
  <pre><code>@echo off
  setlocal enabledelayedexpansion
  for /R %%a in (*_*.png) do (`
    set file=%%a
    ren &quot;!file!&quot; &quot;!file:_=M!&quot;
  )
  pause
  
  setlocal enabledelayedexpansion
  SET old1=My00
  SET new1=_y09
  
  SET old2=My01
  SET new2=_y08
  
  SET old3=My02
  SET new3=_y07
  
  SET old4=My03
  SET new4=_y06
  
  SET old5=My04
  SET new5=_y05
  
  SET old6=My05
  SET new6=_y04
  
  SET old7=My06
  SET new7=_y03
  
  SET old8=My07
  SET new8=_y02
  
  SET old9=My08
  SET new9=_y01
  
  SET old10=My09
  SET new10=_y00
  
  for /R &quot;tokens=*&quot; %%f in ('dir /b *.png') do (
  SET newname=%%f
  SET newname=!newname:%old1%=%new1%!
  SET newname=!newname:%old2%=%new2%!
  SET newname=!newname:%old3%=%new3%!
  SET newname=!newname:%old4%=%new4%!
  SET newname=!newname:%old5%=%new5%!
  SET newname=!newname:%old6%=%new6%!
  SET newname=!newname:%old7%=%new7%!
  SET newname=!newname:%old8%=%new8%!
  SET newname=!newname:%old9%=%new9%!
  SET newname=!newname:%old10%=%new10%!
  move &quot;%%f&quot; &quot;!newname!&quot;
  )
  pause
  
  setlocal enabledelayedexpansion
  SET oldx=Mx0
  SET newx=_x0
  for /R &quot;tokens=*&quot; %%f in ('dir /b *.png') do (
  SET newname=%%f
  SET newname=!newname:%oldx%=%newx%!
  move &quot;%%f&quot; &quot;!newname!&quot;
  )
  pause
  )
  </code></pre>
  <p>FYI,
  Scenario 01:
  (i) 1st <code>FOR LOOP</code> with <code>/R</code> &amp; <code>(*_*.png)</code>
  (ii) 2nd <code>FOR LOOP</code> with <code>/R</code> &amp; <code>(*.png)</code>
  (iii) 3rd <code>FOR LOOP</code> with <code>/R</code> &amp; <code>(*.png)</code></p>
  <p>Result : No changes at all. No files renamed in any way.</p>
  <p>Scenario 02:
  (i) 1st <code>FOR LOOP</code> with <code>/R</code> &amp; <code>(*_*.png)</code>
  (ii) 2nd <code>FOR LOOP</code> with <code>/F</code> &amp; <code>('dir /b *.png')</code>
  (iii) 3rd <code>FOR LOOP</code> with <code>/F</code> &amp; <code>('dir /b *.png')</code></p>
  <p>Result :    (i) &quot;The syntax of the command is incorrect&quot;
  (ii) &quot;File not found&quot;.
  (iii) &quot;File not found&quot;.</p>
  <p>Scenario 03:
  (i) 1st <code>FOR LOOP</code> with <code>/R</code> &amp; <code>(*_*.png)</code>
  (ii) 2nd <code>FOR LOOP</code> with <code>/F</code> &amp; <code>('dir /b /s *.png')</code>
  (iii) 3rd <code>FOR LOOP</code> with <code>/F</code> &amp; <code>('dir /b /s *.png')</code></p>
  <p>Result :    (i) &quot;The syntax of the command is incorrect&quot;
  (ii) Says &quot;1 File moved&quot; to each file.
  (iii) Says &quot;1 File moved&quot; to each file.
  But no changes in the files names.</p>