Apple Event: May 7th at 7 am PT

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

removing a final space from a file/folder name using AppleScript

Hi all,

I'm a network admin with three file servers, all hosting staff shared areas that date back to 2003 or so, sometimes even further. We've started using a backup program that kicks up errors and file/folder duplicates every time it finds a file or folder with a space at the end of the file or folder name. Basically I need to figure out some method of searching all files and folders (and subfolders etc) within a certain directory, finding those that contain a space as the final character and removing that space, without removing all the spaces further back in the name.


I would say I'm an applescript novice, but I'm not even that - I'm less than novice. I'm the novice's apprentice's tea boy. So please help - I can't face manually searching every single last file and folder and renaming them to see if there's a terminal space. My backup program tells me which files they are, but there are over 3000 on one server alone - I have other things I need to do over the next 2 years, lol.


Thanks in advance,


Matt

Posted on Apr 26, 2011 1:35 AM

Reply
Question marked as Best reply

Posted on Apr 26, 2011 6:19 AM

If you are willing to use Applications -> Utilities -> Terminal, you can use:


find /path/to/the/starting/directory -name "* "


Which will find every find under that directory which has a trailing space.

64 replies

Mar 29, 2012 6:09 AM in response to aabilla

The .dmg file is still available via the link posted in

<https://discussions.apple.com/thread/3020422?answerId=15135289022#15135289022>


But here it is again:

<http://dl.dropbox.com/u/147841/remove_trailing_filename_spaces.dmg>


Instructions for usint it are in the README.txt file inside the .dmg. The README.txt also tells you where you can find the actual perl script that does the work inside the Automator .app, so you can verify it will do what you desire.

May 6, 2016 1:51 PM in response to MattLucas1505

And AppleScript with a Ruby handler. It right trims spaces from the file basename, and returns the corrected filename. Assumption is that the extension does not have a trailing space, but that could be handled easily enough. This handler can be called in a repeat loop, if needed. No regular expression syntax necessary.


Before: "~/Desktop/Test/cat dog .txt"

After: "~/Desktop/Test/cat dog.txt"


set fn to POSIX path of (((path to desktop) as text) & "Test:" & "cat dog .txt") as text

set args to fn'squoted form

display dialog "Before: " & fn & return & "After: " & strip_trailing(args) as text

return


on strip_trailing(argv)


return do shell script "ruby <<'EOF' - " & argv & "

# Coding: UTF-8

ARGF.each do

curfile = ARGF.filename

dir = File.dirname(curfile)

x = File.basename(curfile, File.extname(curfile)).rstrip

puts File.join(dir, x + File.extname(curfile))

end

EOF"


end strip_trailing

removing a final space from a file/folder name using AppleScript

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