Skip navigation

Assistance required batch renaming folder names

441 Views 8 Replies Latest reply: Oct 2, 2012 9:46 PM by Jacques Rioux RSS
BGmail Level 1 Level 1 (55 points)
Currently Being Moderated
Sep 15, 2012 10:40 PM

Hi,

 

I wish to rename folders so that it has a space pre-fixed to the beginning of the folder name.  This way folders will show at top in finder. 

 

Does anyone know who to accomplish this in automator or applescript.  I have lloked into Automator but unsure how to do it.  I do not have much experience in Automator or Applescript.

 

Your assistance would be appreciated.

 

Thanks and I look forward to your reply.

iMac (27-inch Mid 2010), OS X Mountain Lion
  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Sep 16, 2012 5:18 AM (in response to BGmail)

    Hi,

     

    Here is an AppleScript that does this

    ------------------------

    property recursive : true -- recursively descends the directory tree.

    -- Important ** set it to false to rename subFolders in the choosen folder only **

     

    set masterFolder to choose folder

    tell application "Finder" to my renameSubFolders((folders of masterFolder whose its name does not start with " ") as alias list)

     

    on renameSubFolders(thesefolders)

        tell application "Finder"

            repeat with i in thesefolders

                set name of i to " " & (get name of i)

                if recursive then

                    tell ((folders of i whose its name does not start with " ") as alias list) to if it is not {} then my renameSubFolders(it)

                end if

            end repeat

        end tell

    end renameSubFolders

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Sep 17, 2012 7:39 PM (in response to BGmail)

    Hi,

     

    I had not thought of that, but the first script will not work if the folder name of have already a space at the beginning.

    Example: if you add a new folder in the subfolder " A", run the script, the new folder will not be renamed because the script is recursive on folders whose name does not begins with a space, which is not the case for the folder " A".

     

     

    Therefore, the script must be recursive on all folders, but it must checks each folder name and rename it if the name does not begin with a space.

     

    BGmail wrote:

     

    Hey Jacques,

     

    Thanks very much for the script.  One question.  Aside from renaming the sub folders how can the script rename the parent folder too.

     

    And how would I be able to rename a folder that OS X will not let me rename due to permissions.  The account I am using is and Admin account.

     

    ...Bruce

     

    Here is the modified script, and I added a shell command to rename the directories (if the Finder was not able to rename these folders), this shell requires an administrator password.

    -----------------------

    property recursive : true -- recursively descends the directory tree.

    -- Important ** set it to false to rename subFolders in the choosen folder only **

     

    set masterFolder to choose folder

    set err to ""

    tell application "Finder"

        set fName to name of masterFolder

        if fName does not start with " " then try

            set name of masterFolder to " " & fName -- rename the parent folder

        on error

            set err to POSIX path of masterFolder

        end try

        set r to my renameSubFolders((folders of masterFolder) as alias list)

    end tell

     

    if err is not "" or r is not {} then -- if the Finder failed to rename  one or more folders

        if r is not {} then

            set oTid to text item delimiters

            set text item delimiters to (character id 0)

            if err is not "" then set end of r to err -- the master folder has not been renamed, add it to list

            set err to r as string -- convert list of folders path to string separated by null character

            set text item delimiters to oTid

        end if

      

        -- ** write folders path to the temporary file **

        set WriteSucceeded to false

        try

            set fPath to (path to desktop folder as text) & "__tempFile__.zxy"

            set f to open for access file fPath with write permission

            set eof f to 0

            write (err & character id 0) starting at eof as «class utf8» to f

            set WriteSucceeded to true

            close access f

        on error

            try

                close access file fPath

            end try

        end try

        if WriteSucceeded then

            -- rename folders with a shell (A dialog will appear asking for the administrator name and password).

            set tempfile to quoted form of POSIX path of fPath

            do shell script "while read -d $'\\0' f;do  d=${f%/*}; name=${d##*/}; /bin/mv -n \"$f\" \"${f%/*/*}/ $name/\"; done<" & tempfile with administrator privileges

           

            do shell script "/bin/rm " & tempfile & " > /dev/null 2>&1 &" -- remove the temporary file

        end if

    end if

     

    on renameSubFolders(thesefolders)

        script o

            property dirToRename : {}

        end script

        tell application "Finder"

            repeat with i in thesefolders

                if recursive then

                    tell ((folders of i) as alias list) to if it is not {} then

                        tell my renameSubFolders(it) to if it is not {} then

                            set end of o's dirToRename to it

                        end if

                    end if

                end if

     

                set fName to name of i

                if fName does not start with " " then try

                    set name of i to " " & fName

                on error -- permissions or others

                    set end of o's dirToRename to (POSIX path of i)

                end try

            end repeat

        end tell

        return o's dirToRename -- returns folders path that renaming has failed with the Finder.

    end renameSubFolders

    -----------------------

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Sep 20, 2012 1:11 PM (in response to BGmail)

    BGmail wrote:

     

    I notice I cannot rename folder such as 'Utilities' in Applications.  Does OS X lock down some folders such that they cannot be changed at all?  Also is it possible that by renaming some folders, will it cause applications not to work?

    It is not recommended to rename items owned by "system", I think it's not possible on newer versions (Lion or newer).

     

     

    I don't know about recent versions of OSX (with Sandboxing and Gatekeeper), because I'm on Leopard (OSX 10.5.8), but on older versions of OSX, all Applications in this folder will work normally, but other applications such as "Software Update" may not update any application in the "Applications" or "Utilities" folder.

  • Jacques Rioux Level 4 Level 4 (1,925 points)
    Currently Being Moderated
    Oct 2, 2012 9:46 PM (in response to BGmail)

    BGmail wrote:

     

    Hi Jacques,

     

    Just wanted to ask you something about the script.  I notice for some folders it keeps prompting for the administrator password.  Is this normal for the script.

     

    ...Bruce

    Yes, this is normal, because the Finder will not rename some folders for various reasons (permissions, folders locked, read only or unknown error).

     

    Now, you want to rename the folders wich are owned by you., you no longer need an administrator password, delete this text  " with administrator privileges  " in your script

     

    The shell will be able to rename folders, if you are the owner, it is not locked and you have write permission.

Actions

More Like This

  • Retrieving data ...

Bookmarked By (0)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.