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

Assistance required batch renaming folder names

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

Posted on Sep 15, 2012 10:40 PM

Reply
8 replies

Sep 16, 2012 5:18 AM in response to BGmail

Hi,


Here is an AppleScript that does this

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

propertyrecursive : true -- recursively descends the directory tree.

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


setmasterFoldertochoose folder

tellapplication "Finder" tomyrenameSubFolders((foldersofmasterFolderwhoseitsnamedoes notstart with " ") asalias list)


onrenameSubFolders(thesefolders)

tell application "Finder"

repeatwithiinthesefolders

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

if recursive then

tell ((foldersofiwhoseitsnamedoes notstart with " ") asalias list) toifitisnot {} thenmyrenameSubFolders(it)

end if

end repeat

endtell

endrenameSubFolders

Sep 16, 2012 3:01 PM in response to Jacques Rioux

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.


Thanks and I look forward to your reply.


...Bruce

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.

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

propertyrecursive : true -- recursively descends the directory tree.

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


setmasterFoldertochoose folder

set err to ""

tellapplication "Finder"

setfNametonameofmasterFolder

iffNamedoes notstart with " " thentry

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

onerror

seterrtoPOSIX pathofmasterFolder

endtry

setrtomyrenameSubFolders((foldersofmasterFolder) asalias list)

endtell


iferrisnot "" orrisnot {} then -- if the Finder failed to rename one or more folders

if r is not {} then

setoTidtotext item delimiters

settext item delimitersto (characterid 0)

iferrisnot "" thensetendofrtoerr -- the master folder has not been renamed, add it to list

seterrtorasstring -- convert list of folders path to string separated by null character

settext item delimiterstooTid

end if

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

setWriteSucceededtofalse

try

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

setftoopen for accessfilefPathwithwrite permission

set eof f to 0

write (err & characterid 0) starting ateofas «class utf8» tof

setWriteSucceededtotrue

close accessf

onerror

try

close accessfilefPath

end try

endtry

ifWriteSucceededthen

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

settempfiletoquoted formofPOSIX pathoffPath

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

endif


onrenameSubFolders(thesefolders)

scripto

property dirToRename : {}

endscript

tell application "Finder"

repeatwithiinthesefolders

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

iffNamedoes notstart with " " thentry

set name of i to " " & fName

onerror -- permissions or others

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

end try

end repeat

endtell

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

endrenameSubFolders

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

Sep 20, 2012 10:32 AM in response to Jacques Rioux

Jacques, Thank you vry much for this script. Works like a charm. One last question. 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?


Thanks again and I look forward to your reply.


...Bruce

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.

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 " withadministrator 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.

Assistance required batch renaming folder names

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