# Applescript: Batch add file extension



## cowofwar (Feb 19, 2002)

Okay, I've got an app that requires the mp3s to have a .mp3 extension for it to read them. However a large number of the mp3 files in my music directory do not have the .mp3 extension. Some do and some do not, they're all mixed together. 

How can I get the finder to open a folder and do the following: If it's a folder, leave it alone, if the file already has a .mp3 extension, leave it alone and if the file does not have a .mp3 extension, append one to the end of the filename. 

I'm really not all that good at applescript and it would be a waste of time for me to add .mp3 manually to every file. 

BTW I'm in mac os x running 10.1.3


----------



## simX (Feb 19, 2002)

IMPORTANT NOTE:  This AppleScript, in its original form, was provided to me courtesy of Apple Computer, Inc., and was provided as "Apple Sample Code".  I am required to tell you that the script I am providing was descendant of "Apple Sample Code", but because I have modified it, it is no longer "Apple Sample Code".  This script is provided, by me and Apple Computer, Inc., AS IS, in its modified form.  If you redistribute this script or modify this script, you must note in the script itself that it was descendant from "Apple Sample Code" but has since been modified and is no longer "Apple Sample Code".

-----

Now that I'm done with the legal stuff, here ya go!  I simply modified Apple's script so that it doesn't add a duplicate suffix or prefix.  I tested it on a sample folder of files and folders, and it did exactly what you wanted.

This script acts on the frontmost window, so make sure you have it active before you run the script -- otherwise it will change everything on the desktop!

Hope this helps. 

P.S.  I hope that was a fast enough response for you. 

P.P.S.  I'd just like to add that AppleScript is f***ing POWERFUL.  I'd classify myself as an "advanced" scripter, and yet I still haven't made as advanced code as some of Apple's provided scripts or the examples in AppleScript Studio.  I want to commend Apple for augmenting AppleScript's power with AppleScript Studio, and I hope that they will continue to develop and support it as a viable programming language.


----------



## cowofwar (Feb 20, 2002)

yay, thank you.

I had actually used a program called file renamer from versiontracker.com, but it wasn't exactly useful since it would add .mp3 to all the folders and to all the files that already had a .mp3


----------



## cowofwar (Feb 20, 2002)

Seems to work, but it doesn't descend into subdirectories within the folders =/


----------



## simX (Feb 20, 2002)

The problem with doing recursive things is if there's aliases, or if there's a bunch of nested folders.  Generally I try to avoid recursive things that do this kind of thing, especially because you can't undo it if you accidentally tell the script to do the wrong thing.

How many folders do you have?  Is it few enough to just run the script a bunch of times in each folder?


----------



## ITz The MaN (Feb 23, 2002)

Thanx man, i've been looking for a script that would do this job for a couple months now. If only I had a good straight forward applescipt guide that just gave the commands and an example... rather than all that bull in the tutorials (i get bored very fast) so I could learn myself to do that stuff.

Maybe you could help me with a problem in your modified script: after renaming about 20 songs, applescript crashes  .


----------



## whendefu (Feb 10, 2010)

hi, tried to download your applescript but it came up as zero k when downloaded ?

you mention about recursive folders, I have many old mp3 files that do not have extensions and they are contained in hundreds of folders, is there any chance you could modify for recursive action ??


----------



## mpsrig (Mar 31, 2010)

Hate to revive an old thread, but your script seems perfect for what I need, and it is undownloadable.  Would you consider putting it up for download somewhere else or maybe just re-attaching it here? Thanks a lot.


----------



## foylyndstrom (Aug 11, 2010)

I second the above posters requests. I'd like the same script.


----------



## Cheryl (Aug 11, 2010)

simX hasn't been around in a while, so I think you are out of luck in getting that file.


----------



## roxics (Aug 16, 2010)

I would also add that I need a script exactly like this. If the original person who attached it is no longer around, does someone else have something like it?


----------



## zenrose (Oct 26, 2010)

And yet another request from here.. PLEASE??!??!  I am desperate.


----------



## Mikuro (Oct 27, 2010)

Just a quick-and-dirty solution:

```
tell application "Finder"
	set the_files to the selection
	repeat with f in the_files
		set ext to the name extension of f
		if the name extension of f is "" and the class of f is document file then
			set the name of f to the name of f & ".mp3"
		end if
	end repeat
end tell
```

Select a bunch of files in the Finder and run the script. It will add .mp3 to any selected files that lack a name extension.

Please note that there is no easy way to undo this.


Edit: On closer look, it seems like you guys want something that will work recursively through a folder. So, here's a slightly modified version that will do just that. To use this one, simply open a folder in the Finder, and the script will change the names of every file within that folder, recursively. *This cannot be undone! It could be very dangerous if you use it on the wrong folder! Use at your own risk!*

```
tell application "Finder"
	display dialog "Are you sure you want to add extensions to every file in '" & the name of the target of Finder window 1 & "'?"
	
	set the_files to every document file of the entire contents of the target of Finder window 1 whose name does not contain "."
	repeat with f in the_files
		set the name of f to the name of f & ".mp3"
	end repeat
end tell
```


----------



## xxx11 (Oct 27, 2010)

The solution seems good


----------

