You can make a difference in the Apple Support Community!

When you sign up with your Apple Account, you can provide valuable feedback to other community members by upvoting helpful replies and User Tips.

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator Batch Rename

I am trying to rename the awful way my video camera names it's files. I want to change it to something that makes sense and has meaning. I am trying to batch rename a bunch of files with names like this:


SANY0001.mp4

SANY0002.mp4

SANY0003.mp4


to


2017-06-01 17_35_58.mp4 (YYYY-MM-DD HH_MI_SS)


I am able to get the Date and Time information from the Create Date using automator, this is not the hard part.


I am not able to replace the SANY0001, SANY0002, SANY0003 because the Replace function needs to search for something, and the only common part is SANY000, witch I can replace, but then I end up with a filename like this:


2017-06-01 17_35_58 1.mp4

2017-06-01 17_41_21 2.mp4

2017-06-01 17_55_02 3.mp4


This is not horrible, but my OCD does not like it!


Is there a way to add a final step in my Automator process that takes the whole original filename, no matter what it is, and replace with null?


My file name is something like 2017-06-01 17_55_02SANY0003.mp4 by the time it gets to the part where I want to replace the SANY0003 with null.


Then I would have the file name I want, 2017-06-01 17_55_02.mp4.


Thank you.


iMac Line (2012 and Later)

Posted on Jun 22, 2020 9:02 AM

Reply
Question marked as Top-ranking reply

Posted on Jun 22, 2020 11:16 AM

I see this as a two action Automator application. At the Automator prompt, choose the folder containing your images to rename. It will rename them with their creation date in the format you requested.


  1. Files & Folders Library : Ask for Finder Items action
    1. Desktop
    2. Folder without multiple selections
  2. Utilities Library : Run Shell Script action
    1. Shell: /bin/zsh
    2. Pass input: as arguments
    3. Replace entire default Run Shell Script content with contents of Script (below)
  3. Save Automator application to your Desktop, and double-click to run.


Script


#!/bin/zsh

: <<'COMMENT'
Provide the starting folder name that contains the images to be
renamed, and the script will rename them in the designated format.

Legend of Zshell expansion and substitution modifiers:
a: - absolute path
h: - directory path
e: - file extension

See man zshexpn(1)

See the following link for the strftime (%) operators used in the stat command:
# https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3
COMMENT

STARTDIR="$1"

# case-insensitve
setopt NOCASE_GLOB
# search in sub-folders for any sany mp4 image files
for f in ${STARTDIR}/**/*.(sany*mp4)(.N);
do
    path="${f:a:h}"
    cdate=$(stat -t '%F %H_%M_%S' -f '%SB' "${f:a}"
    ext="${f:e}"
    # rename SANY00nn.mp4 to yy-mm-dd hh_mm_ss.mp4
    mv "${f:a}" "${path}/${cdate}.${ext}"
done
# resume case sensitive
setopt CASE_GLOB
exit 0


Automator application ready to run screenshot:

Similar questions

3 replies
Question marked as Top-ranking reply

Jun 22, 2020 11:16 AM in response to Enterpriser1701

I see this as a two action Automator application. At the Automator prompt, choose the folder containing your images to rename. It will rename them with their creation date in the format you requested.


  1. Files & Folders Library : Ask for Finder Items action
    1. Desktop
    2. Folder without multiple selections
  2. Utilities Library : Run Shell Script action
    1. Shell: /bin/zsh
    2. Pass input: as arguments
    3. Replace entire default Run Shell Script content with contents of Script (below)
  3. Save Automator application to your Desktop, and double-click to run.


Script


#!/bin/zsh

: <<'COMMENT'
Provide the starting folder name that contains the images to be
renamed, and the script will rename them in the designated format.

Legend of Zshell expansion and substitution modifiers:
a: - absolute path
h: - directory path
e: - file extension

See man zshexpn(1)

See the following link for the strftime (%) operators used in the stat command:
# https://www.freebsd.org/cgi/man.cgi?query=strftime&sektion=3
COMMENT

STARTDIR="$1"

# case-insensitve
setopt NOCASE_GLOB
# search in sub-folders for any sany mp4 image files
for f in ${STARTDIR}/**/*.(sany*mp4)(.N);
do
    path="${f:a:h}"
    cdate=$(stat -t '%F %H_%M_%S' -f '%SB' "${f:a}"
    ext="${f:e}"
    # rename SANY00nn.mp4 to yy-mm-dd hh_mm_ss.mp4
    mv "${f:a}" "${path}/${cdate}.${ext}"
done
# resume case sensitive
setopt CASE_GLOB
exit 0


Automator application ready to run screenshot:

Automator Batch Rename

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.