• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Need to rename files/folders
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Need to rename files/folders


  • Subject: Re: Need to rename files/folders
  • From: Graff <email@hidden>
  • Date: Thu, 10 Jun 2004 01:03:40 -0400

This should do it:
----
property allowedCharacters : "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ$%'-_@~`!{}()#&+,;=[]. "
property replacementCharacter : "!"


tell application "Finder"
try
set theFolder to choose folder with prompt "Select the folder to clean up illegal Windows file names."

set theList to every item of theFolder
repeat while (length of theList is greater than 0)
set theItem to item 1 of theList
if class of theItem is "Folder" then
copy (every item of theItem) to the end of theList
end if
set theList to the rest of theList

set oldName to name of theItem

set newName to my CleanName(oldName)
if (newName is not equal to oldName) then
set containerPath to container of theItem as string
set oldDelims to AppleScript's text item delimiters
set AppleScript's text item delimiters to "."
if (length of text items of newName > 1) then
set theExt to "." & text item -1 of newName
else
set theExt to ""
end if

set AppleScript's text item delimiters to oldDelims
set strippedName to text 1 thru (-1 * ((length of theExt) + 1)) of newName

set i to 0
repeat while ((item (containerPath & newName) exists) and (i < 1000))
set i to i + 1
set newName to strippedName & i & theExt
end repeat

if (i < 1000) then
set (name of theItem) to newName
else
display dialog "The file \"" & theItem & "\" can not have its name cleaned because the cleaned name is already in use."
end if
end if

end repeat
on error
display dialog "An error occured."
end try
end tell

on CleanName(theName)
set newName to ""
repeat with i from 1 to length of theName
if ((character i of theName) is in allowedCharacters) then
set newName to newName & character i of theName
else
set newName to newName & replacementCharacter
end if
end repeat

return newName
end CleanName
----

Mind the line wraps, some long lines might get wrapped. If you want I can mail this script directly to you so that the line wraps don't trip you up.

I made the replacement character for any illegal characters to be an exclamation point (!). If you want it to be something different then change the string of the property "replacementCharacter" to whatever you want it to be. It can even be blank so that the illegal characters will just be stripped and not replaced.

- Ken

On Jun 9, 2004, at 11:00 PM, Bob Bader wrote:

We are moving from our AFP servers to a Celerra (basically a windows
server).
The Mac users will access their data via smb mounts. The issue is that
our Mac users have characters in their file names that are not allowed
on a windows server.
I have been looking for applications, or apple-scripts that will go
though a specified directory and it's sub directories to rename files
and folders by replacing these illegal characters.
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.


References: 
 >Need to rename files/folders (From: Bob Bader <email@hidden>)

  • Prev by Date: On quitting an application
  • Next by Date: Open a remote application
  • Previous by thread: Need to rename files/folders
  • Next by thread: Re: Need to rename files/folders - zsh shell
  • Index(es):
    • Date
    • Thread