I need help grouping files into folders created from filenames

Hi, Can anyone help me with the script as I’ve searched all over and cannot find something to do this.

I have a number of folders of files (1000’s) with generic filenames such as;

collection1-season2-image-dsc001.jpg
collection1-season2-image-dsc002.jpg
collection1-season3-image-dsc001.jpg
collection1-season3-image-dsc002.jpg
collection2-season1-image-dsc001.jpg
collection2-season1-image-dsc002.jpg…etc

I need a script which will examine the filenames and create a series of (un-nested) folders and move (not copy) the files into the folders within the original source folder.

So ideally I end up with my original source folder containing sub-folders such as;

collection1-season2
collection1-season3
collection2-season1 etc…

…with each folder containing any files having a filename starting with the folders’ name.

It would be ideal when starting the script if it would request how many characters of the file names to use when creating the folder names, say from character number 3 to character number 12.

I’ve had problems with folder actions so would rather have a drag N drop solution.

Surely there must be somthing out there already, if not can anyone provide me with some ideas for code (I can do a bit but am pretty inexperienced!)

Any help or advice is much appreciated.

Thanks, Ray

Model: MacminiG4
Browser: Safari 419.3
Operating System: Mac OS X (10.4)

Hi Ray, hopefully this should help you.

set baseFolder to choose folder
set {ATID, AppleScript's text item delimiters} to {"", "-"}
tell application "Finder"
	set theFiles to every file of entire contents of baseFolder as alias list
	repeat with aFile in theFiles
		set fileName to name of aFile
		set {collection, season, theName} to text items of fileName
		set cont to container of aFile as string
		if folder (cont & collection) exists then
		else
			make new folder at cont with properties {name:collection}
		end if
		if folder (cont & collection & ":" & season) exists then
		else
			make new folder at (cont & collection) with properties {name:season}
		end if
		set name of aFile to theName
		move file aFile to (cont & collection & ":" & season & ":")
	end repeat
end tell
set AppleScript's text item delimiters to ATID

At the moment it asks you to choose the base folder when you run it, but making it into a droplet isn’t a big deal. If you need help with that let us know!

The other thing is I didn’t quite understand how you wanted your files named so currently it just strips out the base, collection, and season information leaving you with dsc001.jpg etc, but sorted properly. If you need help renaming the file names just let us know, as well as some more info on it =)

Happy Scripting!

Hi James, Apologies for delay in replying but I wanted to find time to try your script out properly.

Yes it’s doing the right kind of thing but created a few errors looking for “Items”.

Firstly let me say that the filenames can actually be a pretty mixed bag and not always having “-”'s to separate out parts of the filename. Can the script be made to work with just about any filename?

It’s because I have all kinds of odd filenames to sort that I want to be able to stipulate the start and end character position of the filenames for the script to use as rule when creating the folder names (hope that made sense :slight_smile:

THe folders are nested, can they all be placed in the same original/source directory? or can nesting be an option?

I’m not sure I want the original filename stripped back at the moment (but possibly) so I commented out that line (I can do that much :-). It also caused a duplicate files type error - trying to overwrite? Can this be asked for as an option when running tyhe script.

I can get around many of these problems by mass file renaming but it would be some work!

Sounds like I’m ebeing ungrateful doesn;t it - Well I do appreciate your work and can make do withit if you don;t fancy working on it anymore. Your efforts are much appreciated, THnaks Ray

Hi Ray,

No problems in taking some time to test it =)

If I’m on the same page as you what your asking shouldn’t be too difficult. To save us both time though can you post some elaborate examples of your source files and how you would like them to end up?

I think that if I had some better examples of what we are working with here we will reach a better solution for you quicker =)

Hi James, Here goes… I hope I have made myself clear!

Ok here’s an example list of files. Note the filenames could be anything and in any order, typically they would have a sequence of some sort. So I’ve created a fictitious group of files which have “groups” within “groups”. Note: I have left off the file extension.

GroupAbcSubGroupDefViewXYZFile012345-0
GroupAbcSubGroupDefViewXYZFile012345-1
GroupAbcSubGroupDefViewXYZFile012345-2
GroupAbcSubGroupDefViewXYZFile012345-3
GroupAbcSubGroupDefViewXYZFile012345-4
GroupAbcSubGroupDefViewXYZFile012345-5

GroupBBBSubGroupDefViewXYZFile012345-0
GroupBBBSubGroupDefViewXYZFile012345-1
GroupBBBSubGroupDefViewXYZFile012345-2
GroupBBBSubGroupDefViewXYZFile012345-3
GroupBBBSubGroupDefViewXYZFile012345-4
GroupBBBSubGroupDefViewXYZFile012345-5

GroupBBBSubGroupGGGViewXYZFile012345-0
GroupBBBSubGroupGGGViewXYZFile012345-1
GroupBBBSubGroupGGGViewXYZFile012345-2
GroupBBBSubGroupGGGViewXYZFile012345-3
GroupBBBSubGroupGGGViewXYZFile012345-4
GroupBBBSubGroupGGGViewXYZFile012345-5

GroupBBBSubGroupXXXViewAAAFile012345-0
GroupBBBSubGroupXXXViewAAAFile012345-1
GroupBBBSubGroupXXXViewAAAFile012345-2
GroupBBBSubGroupXXXViewAAAFile012345-3
GroupBBBSubGroupXXXViewAAAFile012345-4
GroupBBBSubGroupXXXViewAAAFile012345-5

The script would ask me what part of the filename to use to sort the files and group them into new folders (within the source directory).

So if I entered “from character 1 to character 8” the script would create 2 folders, and move the files into them…

GroupAbc (containing the 6 files starting with “GroupAbc”)
GroupBBB (containing the 18 files starting with “GroupBBB”)

Alternatively if on starting I entered “from character 9 to character 19” the files would be grouped into folders…

SubGroupDef (containing the 12 files with names containing the text “SubGroupDef”)
SubGroupGGG (containing the 6 files with names containing the text “SubGroupGGG”)
SubGroupXXX (containing the 6 files with names containing the text “SubGroupXXX”)

If I was silly enough to enter “from character 27 to character 30” (the part of each filename which says “File”) the script would create a folder called “File” and move every file into that folder. THis of course would be futile but I hope you get my point.

Thanks again for your effort and patience. Ray

Hi Ray,

I think is what your looking for and thanks for that updated description…

set baseFolder to choose folder
set {ATID, AppleScript's text item delimiters} to {"", "-"}
tell application "Finder"
	set theFiles to every file of entire contents of baseFolder as alias list
	
	set s_c to text returned of (display dialog "Please enter the STARTING character:" default answer "")
	set e_c to text returned of (display dialog "Please enter the ENDING character:" default answer "")
	repeat with aFile in theFiles
		set fileName to name of aFile
		set collection to text s_c through e_c of fileName
		set cont to container of aFile as string
		
		if folder (cont & collection) exists then
		else
			make new folder at cont with properties {name:collection}
		end if
		-- The next line trims the file name as so given a file name "GroupAbcSubGroupDefViewXYZFile012345-0"
		-- and entering Start: 9  End: 19  then when the file is moved the new name would be "SubGroupDefViewXYZFile012345-0"
		-- To retain the text prior to the character start (assuming there is any) remove the following line.	
		set name of aFile to (text s_c through -1 of fileName)
		move file aFile to (cont & collection & ":")
	end repeat
end tell
set AppleScript's text item delimiters to ATID

Let me know if that works for you or if you run into any problems.

Hi James, Yes that’s it perfect!

I also tried commenting out the line as you mentioned.

I’ve tested it on a few folders and it works fine, no problems.

A thought… It’s help like this that for me really make the difference in the Mac/PC debate quite apparent. Whilst arguably there’s little between the machines in application functionality, I believe it is the user community that makes the difference. In my opinion PC users are so busy trying tweaking their computers to make them work properly there’s little time to help anyone else in any meaningful manner, a case of the blind leading the deaf! The Mac community is so pleased with their stuff… and that it it works, that there’s generally more of a celebration of the computing environment expressed in terms of helping other realise their expectations.

So once again thanks very much for all your effort.

Regards, Ray