Moving Files into Subfolders based on filenames

Hi all.

I am not a programmer but I’ve been trying to do something simple in AppleScript. Obviously, I’ve been getting it wrong.

Basically, I have a bunch of music in a folder that’s all name according to the standard “artist - title.ext”

I want to move the files into subfolders so that there’s nothing in the root “music” folder, and instead all the files are in music/artist/artist - title.ext

I tried to modify cwtnospam’s script in this thread but, obviously, did something wrong.

tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to every file in thisfolder
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " -"
	repeat with eachfile in mylist
		set filename to name of eachfile
		set artist to text item 1 of filename
		try
			set newfolder to thisfolder & artist as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:artist}
		end try
		try
			set thedestination to make new folder at thisfolder with properties {name:artist}
		on error
			set thedestination to (artist as text)
		end try
		move eachfile to thedestination
	end repeat
end tell

It seems to work until it needs to repeat. I’m sure that to people who know what they’re doing my error is blindingly obvious, but I can’t work it out. This might be, honestly, because I am a little tired and drunk on a Sunday afternoon, but is mostly because I’ve got next to no clue what I’m doing.

Any help would be appreciated.

You were doing fine until you added the “destination” block. It is not needed, as you already created/or specified the subfolder with the newfolder block. Also, is it " - " or " -"?


tell application "Finder"
	set thisfolder to (choose folder) as alias
	set mylist to every file in thisfolder
	set thisfolder to thisfolder as text
	set tid to AppleScript's text item delimiters
	set AppleScript's text item delimiters to " - "
	repeat with eachfile in mylist
		set filename to name of eachfile
		set artist to text item 1 of filename
		try
			set newfolder to thisfolder & artist as alias
		on error
			set newfolder to make new folder at (thisfolder as alias) with properties {name:artist}
		end try
		move eachfile to newfolder
	end repeat
end tell

My take with a bit of explanation:

tell application "Finder"
	set thisfolder to (choose folder) -- as alias not required, choose folder returns an alias
	set mylist to every file in thisfolder as alias list
	set tid to AppleScript's text item delimiters -- save current state
	set AppleScript's text item delimiters to " -" -- text item 1 is everything before this
	repeat with eachfile in mylist
		set filename to name of eachfile -- returns the whole "artist - title.ext"
		set artist to text item 1 of filename -- returns "artist" only
		try -- basically testing to see if there is one; errors if none yet.
			set newfolder to ((thisfolder as text) & artist) as alias
		on error -- file not found
			set newfolder to make new folder at thisfolder with properties {name:artist}
		end try
		move eachfile to newfolder
	end repeat
	set AppleScript's text item delimiters to tid -- set 'em back.
end tell

Brilliant. Not only does that work, I think I even got a little bit of an idea about what I did wrong.

Thank you so much!