batch-editing filenames

Looking for an easy way to remove he first n characters from the beginning of a filename. Using "Rename…" appears to require repeating for each n-character prefix in the folder. I see no way to use wildcards or indicating first- or last- character strings. I run into the same obstacle with Automator.


My UNIX is really rusty, but if the instructions are clear, I'm not afraid to try a shell script (I knew how, once upon a time) in Terminal.


Tnx, Jeff 🤔😉😊

iMac 27″

Posted on Nov 30, 2023 2:49 PM

Reply
Question marked as Top-ranking reply

Posted on Jan 15, 2024 7:15 AM

If one were reckless, the solution could be a UNIX one-liner to iterate through all files and remove the first n characters. Instead, here is a Zsh script that can be run from the Terminal and it takes two arguments:


stripn.zsh 9 ~/Desktop/Test


As it loops through the available files, it tests each one to see if it has more than n characters in the base filename. If so, they are removed and the file is renamed. If not, the file is skipped. More code, more safe guards.


Copy/paste into a plain text editor (not Script Editor) and save. You can run a Zsh script even if your default SHELL remains Bash.


#!/bin/zsh

: <<"COMMENT"
Strip TRIMCHAR count of characters from beginning of filename and rename.

Usage: stripn.zsh nchars foldername
       stripn.zsh 9 ~/Desktop/Test
Reference: https://discussions.apple.com/thread/255314870?sortBy=oldest_first
Tested: macOS Monterey 12.7.2, Zsh v5.8.1
Author: VikingOSX, 2024-01-15, Apple Support Communities, No warranties/indemnity.

Zsh Modifiers (.N): All non-dot files and return null string on failure
                 a: absolute path
                 s: substitute
                 r: drop path and extension
                 t: drop path just filename.ext

COMMENT

# number of characters to strip from base filename
TRIMCHAR=${1}
# if not numeric then error
(( ${TRIMCHAR} )) || {echo "First argument is the number of chars to strip.";exit 1}

# force tilde or just folder name to absolute path
STARTDIR="${2:a:s/\~\//}"

# extensions of files to affect. Modify to suit, or use * alone for all
EXT="(txt|jpg|jpeg|tif)"

cd "${STARTDIR}"

# case-insensitive file handling
setopt nocaseglob
for f in *.${~EXT}(.N);
do
    # determine char length of base filename w/o newline
    LEN=$(echo -n "${f:r}" | wc -m)
    [[ ${LEN} -gt ${TRIMCHAR} ]] || continue

    # cut char 1 - TRIMCHAR from filename resuming with TRIMCHAR + 1 char
    /bin/mv "${f:t}" $(/usr/bin/cut -c $((TRIMCHAR + 1))- <<<"${f:t}")
done
exit 0


10 replies
Question marked as Top-ranking reply

Jan 15, 2024 7:15 AM in response to Jeff Mark

If one were reckless, the solution could be a UNIX one-liner to iterate through all files and remove the first n characters. Instead, here is a Zsh script that can be run from the Terminal and it takes two arguments:


stripn.zsh 9 ~/Desktop/Test


As it loops through the available files, it tests each one to see if it has more than n characters in the base filename. If so, they are removed and the file is renamed. If not, the file is skipped. More code, more safe guards.


Copy/paste into a plain text editor (not Script Editor) and save. You can run a Zsh script even if your default SHELL remains Bash.


#!/bin/zsh

: <<"COMMENT"
Strip TRIMCHAR count of characters from beginning of filename and rename.

Usage: stripn.zsh nchars foldername
       stripn.zsh 9 ~/Desktop/Test
Reference: https://discussions.apple.com/thread/255314870?sortBy=oldest_first
Tested: macOS Monterey 12.7.2, Zsh v5.8.1
Author: VikingOSX, 2024-01-15, Apple Support Communities, No warranties/indemnity.

Zsh Modifiers (.N): All non-dot files and return null string on failure
                 a: absolute path
                 s: substitute
                 r: drop path and extension
                 t: drop path just filename.ext

COMMENT

# number of characters to strip from base filename
TRIMCHAR=${1}
# if not numeric then error
(( ${TRIMCHAR} )) || {echo "First argument is the number of chars to strip.";exit 1}

# force tilde or just folder name to absolute path
STARTDIR="${2:a:s/\~\//}"

# extensions of files to affect. Modify to suit, or use * alone for all
EXT="(txt|jpg|jpeg|tif)"

cd "${STARTDIR}"

# case-insensitive file handling
setopt nocaseglob
for f in *.${~EXT}(.N);
do
    # determine char length of base filename w/o newline
    LEN=$(echo -n "${f:r}" | wc -m)
    [[ ${LEN} -gt ${TRIMCHAR} ]] || continue

    # cut char 1 - TRIMCHAR from filename resuming with TRIMCHAR + 1 char
    /bin/mv "${f:t}" $(/usr/bin/cut -c $((TRIMCHAR + 1))- <<<"${f:t}")
done
exit 0


Jan 14, 2024 5:14 PM in response to Jeff Mark

Niel

User level:

Level 10

726,129 points

Nov 30, 2023 2:54 PM in response to Jeff Mark

Choose Utilities from the Finder's Go menu, open the Script Editor, and run one such as:


tell application "Finder"

set the_folder to folder "tempfolder" of desktop

repeat with this_num from 1 to (count files of the_folder)

set name of file this_num of the_folder to items 2 thru -1 of (get name of file this_num of the_folder) as string

end repeat

end tell

Nov 30, 2023 2:54 PM in response to Jeff Mark

Choose Utilities from the Finder's Go menu, open the Script Editor, and run one such as:


tell application "Finder"

set the_folder to folder "tempfolder" of desktop

repeat with this_num from 1 to (count files of the_folder)

set name of file this_num of the_folder to items 2 thru -1 of (get name of file this_num of the_folder) as string

end repeat

end tell


(247814)

Nov 30, 2023 9:58 PM in response to Niel

Hi Niel,


A couple of clarifying questions, if I way...


I assume that the parenthetical "count files of the_folder" is a command, and not an instruction for me, yes?


Could you please expand on line 4, which seems to be doing most of the heavy lifting. I think I see what it's doing, but I don't see removal happens, and how I would designate, e.g., how many characters to remove? What does "items 2 thru -1" do?

Jan 14, 2024 4:20 PM in response to VikingOSX

HI Vik, as I said to Niel, the project I was on got stalled, and I haven't done anything, but I appreciate your help.


If you could, would you send me a copy of that shell script you mentioned? (Not sure how best to do that. Can you see my emiail? That would work. Or post it here as an attachment?) I might learn something (my UNIX skills have really atrophied). Thanx again.🤔😉😊

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

batch-editing filenames

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