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

Apr 27, 2011 6:34 AM in response to Frank Caggiano

I'll mess around with it and see if I can come up with something before Mat checks it out.

You mean Bob, lol? I'm Matt - I'm the one who can't program for toffee.


And yes, it's bad programming to add a space, although in fairness it's not programming that's at fault in this case, it's the sodding users who somehow appear to feel that an extra space adds a certain je ne sais quoi to their filenames, lol.


Anyway, I have faith in you mere mortals - I'm waiting with baited breath to unleash a fully working script on my shared areas and claim all the praise, lol (ok, ok, I might pass a bit of it on to you guys, lol).

Apr 27, 2011 6:37 AM in response to MattLucas1505

I knew I should have used Perl :-) This script is not very long, but I added a lot of comments to try and expain what I was doing.

OK assuming I understood your new problem, the following perl script might handle both file names with trailing spaces, and filenames with a space between the name and the .type field.


find /path/to/starting/directory -depth -name "* *" | perl -ne ' # Find all files with a space in them
chomp; # remove newline character
if ( /(.*\S)\s+(\.\S+)\s+$/ ) { # find files with spaces before the .type field
#
# The above (...) notation says save what is found in $1
# The 2nd (...) says save what is found in $2
# The .* says match anything
# The \S says match anything except a space
# The \s+ say match 1 or more spaces
# The \. says match . (normally . says match anything, so \. says just .)
# The $ say match end of string
# The \S+ says match 1 or more characters that are not spaces
# The 2 (...) do not include any of the undesired spaces
#
rename $_, $1 . $2; # rename without those spaces
next; # get the next name from the find command
}
if ( /(.*\S)\s+$/ ) { # find files with trailing spaces
#
# The above (...) notation says save what is found in $1
# The .* says match anything
# The \S says match anything except a space
# The \s+ say match 1 or more spaces
# The $ say match end of string
# The (...) does not including any of the trailing spaces
#
rename $_, $1; # rename without the spaces
next; # get the next name from the find command
}
# ignore all remaining files with spaces in them
' # <-- Do not miss the tiny single closing quote :-)


Again, test this script to make sure I did not screw it up. My testing was limited.

Apr 27, 2011 6:57 AM in response to Frank Caggiano

Frank - I don't think that's a possibility, no. There are occasions when staff have tried to put the extension in themselves as part of the filename - it works on Windows (it just recognises it as the extension providing it's correct), but on mac os what you effectively get is something called (for example) file .txt .txt where the first .txt is considered by the OS to be part of the filename and it puts the actual extension in itself (without necessarily displaying it).


There are quite a few examples of filename.xcl or filename.xsl or filename.xl or something where the staff have sort of remembered what the extension is supposed to be and tried to put it in (wrongly) themselves, but again the OS knows what file types they are and sticks the extension on itself (I think that's how it works - I'm a little hazy on exactly how OS X treats file extensions that aren't explicitly added).

Apr 27, 2011 7:11 AM in response to MattLucas1505

You could just copy and paste the script starting with the 'find' command and ending with the last ' and it should just work.


You could use TextWrangler (free and very useful) to create a text file, or the command line 'nano' editor.


If you are going to put the script in a file, then make the very first line


#!/usr/bin/env bash


Which when the script is invoked will specify that the 'bash' shell should be used to interpret it.


After you save the file, set the permissions so it is executable


chmod +x name.of.the.file.with.the.script.in.it


You should then be able to just invoke the script by typing its the path to the file as if it was a command.

Apr 27, 2011 7:23 AM in response to MattLucas1505

oookaay, lol.


Either I'm being a complete idiot, or there really is a bug, lol. Bear with me, I'm very special.....


I copied and pasted the script into Terminal and basically hit Enter (after adjusting the path, obviously).


The result I got was:

Unrecognized character \xC2 in column 1 at -e line 1.


Am I being extra special today? How do I run a text file as a shell script?

Apr 27, 2011 7:38 AM in response to Frank Caggiano

Aah, that'll be why. Guys, I have to disappear - dentists, grrr, lol. Also not going to be around tomorrow (job interview, wish me luck, lol) and Friday someone's getting married - no-one important, lol - just some Prince and his (very attractive) fiancee... Will and Kate or something. Sounds like a TV show to me 😉


Anyway, I'll hit it again on Tuesday (we're having some serious Bank Holiday repetition issues in the UK at the minute, lol) - thanks for all your help thus far - if someone can just pop a quick instruction set on How to Create Shell Scripts For Dummies (TM) for me, then I can hit the ground running on Tuesday.


Cheers all - see you next week.

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.