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.

Script or dmg to remove a trailing space at the end of folders and file names

I’m looking to find an automated function to remove a trailing space from 1000s of files and folders on my old Mac. Not sure how it’s happened but I want to sync these old files to One Drive which it can’t currently.


There is an old post here that solved the issue. Bob Harris created a dmg to drop the files into.

it’s not available anymore.


removing a final space from a file/folder… - Apple Community




Posted on Sep 18, 2024 12:37 AM

Reply
Question marked as Top-ranking reply

Posted on Sep 18, 2024 12:38 PM

Here is a Zsh script that has been tested on macOS Sequoia v15.0 and Zsh v5.9. The script's only command-line argument is the parent folder to start the recursive matching and renaming of files and folders that possess one or more trailing spaces after the name. This script will not remove spaces that punctuate file or folder names.


#!/bin/zsh

: <<"COMMENT"
Provide the parent folder of the file/folder hierarchy where there may be files or
folders with trailing whitespace after their names. This script will rename them
without any trailing space(s).

Usage: rms.zsh ~/Folder
       rms.zsh /full/path/to/Folder

Tested: macOS Sequoia v15.0, Zsh v5.9
VikingOSX, 2024-09-18, Apple Support Communities, no warranties at all.
COMMENT

STARTFOLDER="${1:a:s/\~\//}"

function stringTrimmer () {
    # only trim leading or trailing spaces from filename
    osascript <<EOF
    use framework "Foundation"
    use scripting additions

    property ca : current application

    set notrailing_spaces to ca's NSCharacterSet's whitespaceCharacterSet
    return ((ca's NSString's stringWithString:"${1}")'s stringByTrimmingCharactersInSet:notrailing_spaces) as text
EOF
}

setopt nocaseglob glob_star_short
# recursively reverse sort by name so files get processed before folders
for f in $STARTFOLDER/**(On);
do
    # looking for a file or folder path ending with one or more spaces
    [[ ${f:a} =~ ".*([ ]*)$" ]] || continue
    # rename file or folder without trailing spaces, but do not (-n) overwrite existing file/folder
    /bin/mv -n "${f:a}" "$(stringTrimmer "${f:a}")" 2> /dev/null
done
exit 0



2 replies
Question marked as Top-ranking reply

Sep 18, 2024 12:38 PM in response to Jo_mcg

Here is a Zsh script that has been tested on macOS Sequoia v15.0 and Zsh v5.9. The script's only command-line argument is the parent folder to start the recursive matching and renaming of files and folders that possess one or more trailing spaces after the name. This script will not remove spaces that punctuate file or folder names.


#!/bin/zsh

: <<"COMMENT"
Provide the parent folder of the file/folder hierarchy where there may be files or
folders with trailing whitespace after their names. This script will rename them
without any trailing space(s).

Usage: rms.zsh ~/Folder
       rms.zsh /full/path/to/Folder

Tested: macOS Sequoia v15.0, Zsh v5.9
VikingOSX, 2024-09-18, Apple Support Communities, no warranties at all.
COMMENT

STARTFOLDER="${1:a:s/\~\//}"

function stringTrimmer () {
    # only trim leading or trailing spaces from filename
    osascript <<EOF
    use framework "Foundation"
    use scripting additions

    property ca : current application

    set notrailing_spaces to ca's NSCharacterSet's whitespaceCharacterSet
    return ((ca's NSString's stringWithString:"${1}")'s stringByTrimmingCharactersInSet:notrailing_spaces) as text
EOF
}

setopt nocaseglob glob_star_short
# recursively reverse sort by name so files get processed before folders
for f in $STARTFOLDER/**(On);
do
    # looking for a file or folder path ending with one or more spaces
    [[ ${f:a} =~ ".*([ ]*)$" ]] || continue
    # rename file or folder without trailing spaces, but do not (-n) overwrite existing file/folder
    /bin/mv -n "${f:a}" "$(stringTrimmer "${f:a}")" 2> /dev/null
done
exit 0



Script or dmg to remove a trailing space at the end of folders and file names

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