Question on batch renaming

Discussion in 'Mac Programming' started by zancine, Mar 14, 2018.

  1. zancine macrumors newbie

    zancine

    Joined:
    Mar 14, 2018
    #1
    Hi there, I have quite many images files need to be named following a pattern, and the pattern goes like:

    Section_1_1_NAME.jpg
    Section_1_2_NAME.jpg
    Section_1_3_NAME.jpg
    Section_1_4_NAME.jpg
    Section_1_5_NAME.jpg

    Section_2_1_NAME.jpg
    Section_2_2_NAME.jpg
    Section_2_3_NAME.jpg
    Section_2_4_NAME.jpg
    Section_2_5_NAME.jpg

    ....and so on, where NAME keep being the same all the way.
    The tricky part is for every 5 items, the first number increase by 1.
    The original file names are in sequence, e.g. "file.001.jpg"

    I am not a programmer but I guess this can be done with terminal to run commands and batch rename a whole folder of images?
    I tried with automator but can't really figure out how to get that tricky part done.

    It'd be a life saver if anyone can come up with a solution, as I have a lot of files need to be named that way.

    Thanks!
     
  2. elemenohme macrumors regular

    Joined:
    Nov 6, 2014
    #2
  3. Toutou macrumors 6502a

    Toutou

    Joined:
    Jan 6, 2015
    Location:
    Prague, Czech Republic
    #3
    Copy this into terminal, press enter. DO NOT RUN ON YOUR ORIGINAL FILES, make a backup. This script renames everything, including any folder inside the target folder. So better create a new one and put your files inside it.
    Code:
    bash -c 'clear;echo "Please drag n drop the target folder into this window.";echo "Should look like this: /path/to/folder";echo -n "Input: ";read folder;cd $folder;if [ $? -ne 0 ];then echo "Wrong input, cant access directory, aborting";read;exit;fi;I=0;for filename in *;do let "A=(I/5)+1";let "B=I%5+1";newname="Section_$A""_$B""_$filename";echo "Renaming $filename to $newname";mv "$filename" "$newname";let "I++";done;echo "---All done!---";read'
     
  4. zancine thread starter macrumors newbie

    zancine

    Joined:
    Mar 14, 2018
    #4
    Thanks a lot Toutou, that works like a charm!
    Just amazing it all get done in a blink.

    Thank you so much !!!!!
     

Share This Page

3 March 14, 2018