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 11:01 AM in response to MattLucas1505

A great book for learnen' the Unix command line.


Practical Guide to Linux Commands, Editors, and Shell Programming, A (Paperback) Third edition includes Mac OS X the book


To see all extensions in the finder:

finder > prefernces

click on the advanced tab

check the box that says to show all extensions. Mostly good except that applications will show up with the .app extension.


Unix doesn't really have extensions. It's a convention picked up from other Operating Systems. So when you say filename, it will include the extension. There is a command for figuring out the type of data a file contains:


Macintosh-HD -> Applications -> Utilities -> Terminal

mac $ file MacScripter\ .pdf

MacScripter .pdf: PDF document, version 1.3

mac $


The Mac OS X finder does have extensions.


Robert

Apr 30, 2011 12:11 PM in response to MattLucas1505

If you download and install the Satimage osax so that you can run regular expressions in AppleScript, the following script should do what you need:


tell application "System Events"

set theFolder to folder "path:to:folder"

end tell

runItems(theFolder)


on runItems(theItem)

tell application "System Events"

if class of theItem is folder then

set folderContents to (every disk item of theItem whose visible is true)

repeat with thisItem in folderContents

my runItems(thisItem)

end repeat

end if

set oldName to name of theItem

set newName to change {"\\s+$", "\\s+(\\.[a-zA-Z0-9]+)$"} into {"", "\\1"} in oldName with regexp

if oldNamenewName then set name of theItem to newName

end tell

end runItems


Darn useful osax, that - I highly recommend it.

May 3, 2011 12:27 AM in response to BobHarris

Guys - morning - thanks for the extra posts over the looooong weekend, lol. Will and Kate did get married - who'd have guessed, lol, and I did get the job. Happy days.


Ok - Bob - I've edited the script to reflect the correct path and I've run the chmod command to make it executable (cd to parent directory of the .sh file and enter the chmod command), however whenever I try to run it, it just wants to open it in textedit. I've tried copying and pasting the contents into terminal, but again no joy - I realise that's not how it's supposed to work but it would be daft not to try everything I can think of before coming back to you lot and asking again.


What am I missing? I'm beginning to feel like the village idiot, lol.

May 3, 2011 3:11 AM in response to John Maisey

Hmm, tried that, but now it seems to have no effect - it appears to run in Terminal (well, no errors, and just displays the command prompt), however it's not having any effect on the files and folders that it's supposed to.


I've triple-checked the path is correct - beginning of the script now reads:


#!/usr/bin/env bash


EDIT_ME_STARTING_DIR="/Shared Items/test/"

EDIT_ME_STARTING_DIR="."


find "$EDIT_ME_STARTING_DIR" -depth -name "* *" | perl -ne ' # Find all files with a space in them


If I understand correctly, the " " around the path make it unnecessary to render spaces in paths with a \ (i.e. ...="/Shared\ Items/test/"


However, I've tested the path in the two additional formats:

EDIT_ME_STARTING_DIR="/Shared Items/test" (note: lack of final /)

EDIT_ME_STARTING_DIR="/Shared\ Items/test/"


and none of the three formats are removing the spaces at all, so I'm obviously still doing something wrong.<


<Edit> Given of course that I know the script was at least partially working previously, it's obviously me, not the script </Edit>

May 3, 2011 4:43 AM in response to MattLucas1505

Following on from above.


I'm not too hot on regex, so forgive me if I'm mistaken. There appears to be two cases where this script won't work correctly.


  1. Where there is a directory with a trailing space with a file inside that also has a trailing space. The directory will be renamed and the file will not be where it is expected to be when the script comes to that result.
  2. The script renames items with a trailing space ("/path/ending/in/space ") including files with space at the end ("/path/to/file.txt "), and also items with a space before the final dot as well as a space at the end (/path/to/file .txt "), but not to a file with the space before the dot with none after (/path/to/file .txt").


Best wishes

John M

May 3, 2011 4:54 AM in response to John Maisey

Aaaah, right. That appears to be the situation I'm in right now.


Case 1 *should* be resolved by running the script a second time, am I right?


Case 2 - since it doesn't seem to be possible to have a file where the space comes *after* the extension (since that situation could only arise if the author put the extension in as part of the filename and hence the file would have a normal extension following that anyway, whether it's visible or not), that means that this script falls down on finding examples such as:


"filename .ext"


which is in fact what I have quite a lot of. Ok, so if I modify the path to run at the root of the problem directory (/Shared Items/ in this case), and then run it again, it should catch a fairly high percentage of the problem directories, but almost certainly won't catch the problem files (since they all fall into case 2 - even if the extension isn't displayed, it is in fact there).


Well, something is better than nothing, certainly, and if anyone can suggest anything better (I'll have a shot with the AppleScript version twtwtw posted too), it will be gratefully received.


Virtual beers, anyone?

May 3, 2011 5:18 AM in response to MattLucas1505

Hi Matt,


MattLucas1505 wrote:


Aaaah, right. That appears to be the situation I'm in right now.


The script below fixes my second point, but not my first, which would involve stepping through the results of the find command in reverse (something I'm sure someone else will be able to provide the solution to).


#!/usr/bin/env bash

EDIT_ME_STARTING_DIR="/path/to/starting/directory"

find "$EDIT_ME_STARTING_DIR" -depth -name "* *" | perl -ne ' 
    chomp;  

    if ( /(.*\S)\s+(\.\S+)$/ ) { 
        rename $_, $1 . $2;      
        next;                   
    }

    if ( /(.*\S)\s+$/ ) {       
        rename $_, $1;          
        next;                   
    }
'



MattLucas1505 wrote:


Virtual beers, anyone?

If you're buying. 😉


Best wishes

John M

May 3, 2011 5:37 AM in response to John Maisey

Another version that should read the result in reverse and therefore sort out items before their containers. This should fix my first point above.


#!/usr/bin/env bash

EDIT_ME_STARTING_DIR="/path/to/starting/directory"

find "$EDIT_ME_STARTING_DIR" -depth -name "* *" | tail -r | perl -ne '
    chomp;

    if ( /(.*\S)\s+(\.\S+)$/ ) {
        rename $_, $1 . $2;
        next;
    }

    if ( /(.*\S)\s+$/ ) {
        rename $_, $1;
        next;
    }
'

May 3, 2011 5:56 AM in response to MattLucas1505

MattLucas1505 wrote:


.... this script falls down on finding examples such as:


"filename .ext"



Are you using "do shell script find"? If so, find has a -regex option (or better yet, case insensitive: -iregex), so:

find path/to/the/starting/directory -iregex '.* \..*'


should work, however, it will give a false positive to a filname with a '.' in its name, such as "Create a .zip archive.txt", so some tweaking of the regex is needed.


Tony

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.