Changing file extention into uppercase

hi,

Is it possible to change the file extention of all files within a specific folder into uppercase?.

Hi kshyam,

here my attempt:

set theFolder to (choose folder)

tell application "Finder"
	repeat with fil in (get files of theFolder)
		set nam to (name of fil)
		if (nam contains ".") then
			set n_ext to name extension of fil
			set ext_len to (length of n_ext) + 1
			set new_name to ((characters 1 thru (-1 * ext_len) of nam) as string) & (my toUpper(n_ext))
			set name of fil to new_name
		end if
	end repeat
end tell

on toUpper(theString)
	return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")
end toUpper

D.

Hello

With the original script. I got these odd results:
set name of document file “lignesClément.cwk” of folder “matin brun, autres documents à exploiter 2” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk to “lignesCleCWK”
“lignesCleCWK”
set name of document file “lignesKlément-lignesKlément ++++++.cwk” of folder “matin brun, autres documents à exploiter 2” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk to “lignesKleCWK”
“lignesKleCWK”

So I edited it

set theFolder to (choose folder)

tell application "Finder"
	repeat with fil in (get files of theFolder)
		set nam to (name of fil)
		if (nam contains ".") then
			set n_ext to name extension of fil
			set ext_len to (length of n_ext) + 1
			set new_name to ((text 1 thru (-ext_len) of nam) as string) & (my toUpper(n_ext))
			set name of fil to new_name
		end if
	end repeat
end tell

on toUpper(theString)
	return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")
end toUpper

The important change is that I replaced “character” by “text”. The removing of the “hard coded” multiplication by the “implicit” one is quite cosmetic.
Now it works correctly.

set name of document file “lignesClément.cwk” of folder “matin brun, autres documents à exploiter 2” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk to “lignesClément.CWK”
set name of document file “lignesKlément-lignesKlément ++++++.cwk” of folder “matin brun, autres documents à exploiter 2” of folder “Desktop” of folder “yvankoenig” of folder “Users” of startup disk to “lignesKlément-lignesKlément ++++++.CWK”

Yvan KOENIG (from FRANCE vendredi 6 octobre 2006 12:11:25)

Merci Yvan,

it’s a Unicode problem - I think coercing nam to class ‘string’* would also have solved this:

set theFolder to (choose folder)

tell application "Finder"
   repeat with fil in (get files of theFolder)
       set nam to (name of fil) as string -- <- *here
       if (nam contains ".") then
           set n_ext to name extension of fil
           set ext_len to (length of n_ext) + 1
           set new_name to ((characters 1 thru (-1 * ext_len) of nam) as string) & (my toUpper(n_ext))
           set name of fil to new_name
       end if
   end repeat
end tell

on toUpper(theString)
   return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")
end toUpper

btw.: in case s.o. needs a lowercase version:


on toLower(theString)
	return (do shell script "echo " & quoted form of theString & " | tr \"[:upper:]\" \"[:lower:]\"")
end toLower

D.

Hello

I knew that it was an unicode problem and I forgot one change.

As the system/Finder is heavily using Unicode, I think that it would be safer to use the script in full Unicode as this one does:


set theFolder to (choose folder)

tell application "Finder"
	repeat with fil in (get files of theFolder)
		set nam to (name of fil)
		if (nam contains ".") then
			set n_ext to name extension of fil
			set ext_len to (length of n_ext) + 1
			set new_name to ((text 1 thru (- ext_len) of nam) as Unicode text) & (my toUpper(n_ext))
			set name of fil to new_name
		end if
	end repeat
end tell

on toUpper(theString)
	return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")
end toUpper

As far as I know, there is no need for an Unicode savyy lower2upper handler as extension names doesn’t use accented chars.

Yvan KOENIG (from FRANCE vendredi 6 octobre 2006 13:11:4)

Thanks Dominik and Yvan,

When i ran the script and selected a folder i got the following message

Finder got an error: The operation could not be completed because there is already an item with that name

Hi kshyam,

maybe your volume has a format that makes a difference between upper& lower case in file names and you have files with identical (except case) names (filexy.jpg and filexy.JPG)

In this case you could try this (adds an index ‘filexy.JPG’, ‘filexy_1.jpg’ to duplicates):

set theFolder to (choose folder)

tell application "Finder"
	repeat with fil in (get files of theFolder)
		set nam to (name of fil)
		if (nam contains ".") then
			set n_ext to name extension of fil
			set ext_len to (length of n_ext) + 1
			set new_name to ((text 1 thru (-ext_len) of nam) as Unicode text) & (my toUpper(n_ext))
			set idx to 1
			repeat while (exists file new_name of theFolder)
				set new_name to ((text 1 thru (-ext_len) of nam) as Unicode text) & "_" & idx & (my toUpper(n_ext))
				set idx to idx + 1
			end repeat
			set name of fil to new_name
		end if
	end repeat
end tell

on toUpper(theString)
	return (do shell script "echo " & quoted form of theString & " | tr \"[:lower:]\" \"[:upper:]\"")
end toUpper

An alternative approach (though I can’t imagine why you want the extensions in upper case):


property lc : "abcdefghijklmnopqrstuvwxyz"
property uc : "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

tell application "Finder"
	set F to files of (choose folder)
	repeat with aF in F
		set NE to name extension of contents of aF
		set NF to name of contents of aF
		set CE to count NE
		set newE to ""
		repeat with k from 1 to CE
			if character k of NE is in lc then
				set newE to newE & character (offset of (character k of NE) in lc) of uc -- switch to upper
			else -- already upper case
				set newE to newE & character k of NE -- leave as it was
			end if
		end repeat
		set name of contents of aF to (text 1 thru -(CE + 1) of NF) & newE
	end repeat
end tell

[EDIT] This can be substantially cleaned up and several variables eliminated, but it works on my machine as is

Thanks Adam Bell,

I tried your approach also, but when i select a folder i still get the message “The operation could not be completed because there is already an item with that name”

Hello

CAUTION

The lowerToUpper code given by Adam Bell works correctly only if the string doesn’t contain accented chars.
This is true (att least at this time) for name extensions but it may be wrong for file/folder names.

And of course, here it doesn’t take care to the fact that some OS treat xx.ABC as different from xx.abc.

So, if the two already exist, the script will get an error when trying to rename xx.abc as xx.ABC :wink:

Yvan KOENIG (from FRANCE lundi 9 octobre 2006 12:24:14)

Good point Mr. K. :).

Of course I didn’t test for that, but an ‘on error’ handler could make a list of failed conversions, and/or change the label colour of files that were not altered so you could examine them after the run. On my machine, capitalization is ignored: jpg, Jpg, JPG, etc. are all the same extension, and, of course, I don’t have accented extensions or extensions not included in the alphabets in my script either, so I didn’t test for any of that.