Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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 26, 2011 6:43 AM in response to MattLucas1505

Hi,


Using the same find command and then mv and wrapping it in Applescript.


Best wishes

John M


set myFolder to "/path/to/the/starting/directory" -- << Change this line.

set thePaths to do shell script "/usr/bin/find " & quoted form of myFolder & " -name '* '"

repeat with thisLine in paragraphs of thePaths

set theDestination to (characters 1 thru -2 of thisLine) as text

do shell script "/bin/mv " & quoted form of thisLine & " " & quoted form of theDestination

end repeat

Apr 26, 2011 7:59 AM in response to John Maisey

John, have tried that (obviously changing the path - created a test directory filled with a mix of files and folders some with terminal spaces, some without), however it's applescript editor returned this error when I ran the script:


error "mv: rename /Shared Items/test/test middle and end /Interim folder endspace to /Shared Items/test/test middle and end /Interim folder endspace: No such file or directory" number 1


Colin and Bob, going to try your two suggestions now, thanks...

Apr 26, 2011 8:09 AM in response to BobHarris

Bob, just tried this, using this syntax:


find /Shared\ Items/test -name "* " | awk '

{

nospace = $0

sub(/ *$/,"",nospace)

system( "mv " $0 " " nospace )

}

'

however am getting the error from Terminal:


-bash: : command not found


Obviously I've double-checked the path - it's on the boot drive, the top level folder is "Shared Items" and the test folder is "test", so I'm pretty sure that part of the syntax is correct, is it not?

Apr 26, 2011 8:29 AM in response to Frank Caggiano

Frank,

I've tried running the following in Terminal on the relevant server (10.6 Server).


find /Shared\ Items/test -name "* " | awk '

{

nospace = $0

sub(/ *$/,"",nospace)

system( "mv " $0 " " nospace )

}

'


Also tried the following in AppleScript Editor and running within the editor (clicking the "Run" button):


set myFolder to "/Shared Items/test"


set thePaths to do shell script "/usr/bin/find " & quoted form of myFolder & " -name '* '"


repeat with thisLine in paragraphs of thePaths

set theDestination to (characters 1 thru -2 of thisLine) as text

do shell script "/bin/mv " & quoted form of thisLine & " " & quoted form of theDestination

end repeat


Shed any light???


Thanks for the assistance on this guys.

Apr 26, 2011 9:14 AM in response to MattLucas1505

Hi Matt,


I see what happened, it changed the folder before the sub-folder causing the error, as it could not find the folder at the path any more.


Try this which processes the list in reverse order:


set myFolder to "/path/to/the/starting/directory" -- << Change this line.

set thePaths to do shell script "/usr/bin/find " & quoted form of myFolder & " -name '* '"

repeat with thisLine from (count paragraphs of thePaths) to 1 by -1

set theSource to (paragraphthisLine of thePaths)

set theDestination to (characters 1 thru -2 of theSource) as text

do shell script "/bin/mv " & quoted form of theSource & " " & quoted form of theDestination

end repeat




Best wishes

John M

Apr 26, 2011 10:27 AM in response to MattLucas1505

I just realized there is "Bug" in my code :-(


I did not protect the file name spaces from the /bin/sh shell which the system() function passed the mv command to.


Try the following:


find /Shared\ Items/test -name "* " | awk -v Q="'" '
{
nospace = $0
sub(/ *$/,"",nospace)
system( "mv " Q $0 Q " " Q nospace Q )
}
'


This is a bit ugly. Maybe I should have used Perl :-)


The -v Q="'" is creating an awk variable with a single quote in it. I'll use the single quote to protect the spaces in the file name.


The sub() is finding the trailing spaces and removing them from the variable nospace which was set from $0 where awk stores each line read.


The system() function passes the comand text string to the /bin/sh Unix shell to be executed. In our case we are using awk string concatenation to form a string that looks like:


mv 'original file with spaces including at the end ' 'original file with spaces including at the end'


The Q is providing the ' characters.


The 3 awk commands are executed once for every file name with a trailing space found by the 'find' command.

Apr 27, 2011 4:48 AM in response to BobHarris

Bob - thanks a lot - we're almost there. It's worked fine for all the folders that have terminal spaces, but there are a number of files that also have a space at the end, and it's not worked for any of those.


Folder structure is:


/Shared Items/test/test middle and end /interim folder endspace /docspace .txt/


Everything up to the final text file is renamed perfectly to just remove the final space. However that final text file doesn't get renamed (there are a few of these in different subdirectories and none of them have been).


However, I have to say, I am in complete awe of the scripting, lol - as far as I'm concerned you might as well be speaking Martian, lol. Told you - novice's apprentice's tea boy.

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

are a number of files that also have a space at the end, and it's not worked for any of those.


Folder structure is:


/Shared Items/test/test middle and end /interim folder endspace /docspace .txt/


Everything up to the final text file is renamed perfectly to just remove the final space. However that final text file doesn't get renamed (there are a few of these in different subdirectories and none of them have been).


The path you're showing doesn't show a space at the end of the text file (its suppose to be docspace.txt, correct?) In fact what in the path you've shown the last element is a directory (trailing /)

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

Frank - ".../docspace .txt" is what I put, not ".../docspace.txt". The difference being there is a space between the end of the filename and the extension and this is not getting removed by the script.


Sorry - just got what you meant about the trailing / - you're quite right - my syntax error, lol. The path is actually .../docspace .txt


This is one of the things that's causing me problems. As you all know, known document types aren't required by OS X to have an extension (at least not visibly), and in some cases that's what we have. In actual fact, as far as Finder is concerned on the server in question, the actual path is ".../interim folder endspace/docspace " (still with a trailing space, but no extension, and (thanks Frank) no trailing /).


I don't know whether it's an issue or not programmatically speaking (is that a word?) - i.e. the fact that some documents are filename(space).extension and others are just filename(space)


Regardless, I need to fix it, and I need help from you scripting guru's out there cos I'm about as much use as a chocolate teapot when it comes to scripting.

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

Just saw the space after docspace and before the extension, the trailing slash threw me off.


Thats going to be a bit trickier (at least for us mere mortals) I'll mess around with it and see if I can come up with something before Mat checks it out.


It is an issue and poor programming practice to add a space to the end of things if it is unintentional. In a lot of situations the programer can get away with it but not when dealing with filenames (as you are painfully finding out)


One of the old tricks to drive novice Unix Sys Admins mad was to imbed non-printing characters into filenames and watch them try to figure out how to get fid of the offending file, highly entertaining.

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.