For loop recursively through folder in windows cmd for file rename

10,097

I think this is may suit your case.

for /R %i in (*.jpg) DO convert %i -thumbnail 30% %~di%~pi%~ni_small%~xi
Share:
10,097
Andrija
Author by

Andrija

Software developer

Updated on June 07, 2022

Comments

  • Andrija
    Andrija almost 2 years

    I'm trying to iterate through folders with images to create thumbnails using ImageMagic, and rename thumbnail files with small_ prefix.

    when I execute this in single folder, it works great:

    FOR %a in (*.jpg) DO convert %a -thumbnail 30% small_%a
    

    To loop through subfolders, I just need /R flag:

    FOR /R %a in (*.jpg) DO convert %a -thumbnail 30% small_%a
    

    This will result into new name for thumbnail small_c:\images\image.jpg which is wrong :)

    How can I get small_ prefix into file name while recursing through subfolders in script, i.e. from c:\images\image.jpg to c:\images\small_image.jpg ?

    Thanks.

  • Andrija
    Andrija about 13 years
    I'll just use small_ in front of file name: for /R %i in (*.jpg) DO convert %i -thumbnail 30% %~di%~pismall_%~ni%~xi
  • Peter Perháč
    Peter Perháč over 9 years
    this would not work for me no matter what, until i found in the manual ss64.com/nt/for.html that the correct syntax is double-%, e.g.: for /R %%i in (*.jpg) DO convert %%i...
  • naitsirhc
    naitsirhc over 7 years
    The double-% is only necessary if running the command in a batch file. If running the for loop at the command prompt, only a single % is required.