Creating Folders With Filenames

Hello everyone I currently have a script that creates folders for files and places them in their respective folders according to filename. However I would like to tweak it a bit so that the extensions of a file does not show in the folder name: for example dog.jpg (filename) → dog.jpg (folder name) becomes: dog.

I also have the folders being created in a new location. I would like to create the folders without having to move them and leave them where they are… Ultimately, I would like to set this up as a folder action. So if I had a list of 20 files I can just select them an enable them to create a folder.

Can this script be created with a conditional of: if the files end with .eps, .ai, .jpg, .gig name the folder before these extensions or if there is no extension name it as it it???
Thanks!!!

This is what I have:

on open theFiles
set DestinationFolder to (choose folder with prompt “Choose Destination Folder”)
repeat with ThisFile in theFiles
tell application “Finder”

		set Filename to (name of (info for ThisFile))
		
		set FoldersName to (Filename) as string
		
		make new folder at DestinationFolder with properties {name:FoldersName}
		
		move ThisFile to folder (DestinationFolder & FoldersName as string)
		
	end tell
end repeat

end open

ryo:

Try this and report back:

set DestinationFolder to (choose folder with prompt "Choose Destination Folder")
set Bad_Extensions to {"eps", "ai", "jpg", "gig"}
repeat with ThisFile in theFiles
	tell application "Finder"
		
		set {Filename, File_Ext} to {(name of (info for ThisFile)), (name extension of (info for ThisFile))}
		if Bad_Extensions contains File_Ext then
			set Filename to (characters 1 thru ((offset of "." in Filename) - 1) of Filename) as string
		end if
		set FoldersName to (Filename) as string
		
		make new folder at DestinationFolder with properties {name:FoldersName}
		
		move ThisFile to folder (DestinationFolder & FoldersName as string)
		
	end tell
end repeat

Hey Craig I am getting a script error?: The variable theFiles is not defined.

Thanks

ryo:

Sorry, I forgot to include the droplet stuff when I was finished. How about this:

on open theFiles
	set DestinationFolder to (choose folder with prompt "Choose Destination Folder")
	set Bad_Extensions to {"eps", "ai", "jpg", "gig"}
	repeat with ThisFile in theFiles
		tell application "Finder"
			
			set {Filename, File_Ext} to {(name of (info for ThisFile)), (name extension of (info for ThisFile))}
			if Bad_Extensions contains File_Ext then
				set Filename to (characters 1 thru ((offset of "." in Filename) - 1) of Filename) as string
			end if
			set FoldersName to (Filename) as string
			
			make new folder at DestinationFolder with properties {name:FoldersName}
			
			move ThisFile to folder (DestinationFolder & FoldersName as string)
			
		end tell
	end repeat
end open

Hey Craig the script is sweet! Is there anyway to make the droplet automatically create the folders in the folder/place the files are being dragged from? So there will not be a “Choose Destination Folder?”

Thanks Again!

Glad to hear it is working for you. I don’t think it is such a great idea to try to retain the original folder. When you use a filename to create a folder in the same location and the same name, it is going to error out, because the UNIX underpinnings for the folder and file building will not allow it. It is important to remember that UNIX (upon which OS X is built) makes no distinction between folders and files; they are all files as far as the OS is concerned. A folder is nothing more than a file that contains other files, therefore, you are not allowed to create a new folder using a filename that already exists in that location.

Could you elaborate on the ‘big picture’ of what you are trying to do? It is possible that someone will have an idea that you may not have considered to accomplish your overall task(s).