Creating SubFolders using part the file name between underscores

I’m fairly new and I have received some great help from these forums. I have a question that maybe someone can help me out.
I would like to create a script that sorts images into subfolders.
For example I have files that are named like this
Date_Test1_Group13_001.jpg
Date_Test14_Group2_050.jpg
Date_Test2_Group1_061.jpg
Date_Test20_Group1_071.jpg
etc.

What I need is to sort the images into the first folder which would be the date and that will vary. (Date) 012506 or Jan25
Next is in the date folder to create subfolders by what is between the first and second underscore. again this will vary. (Test14)
Last is to create SubSubfolders in the Subfolder by the text in between the second and third underscore.
Thanks in advance.

MacAttack4

Assuming you know how to make folders and duplicate stuff to them, here’s how you break up the names so you can do it:

set nameList to {"012506_Test1_Group13_001.jpg", "012506_Test14_Group2_050.jpg", "020406_Test2_Group1_061.jpg", "020406_Test20_Group1_071.jpg"}

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
repeat with aName in nameList
	set contents of aName to text items of aName
end repeat
set AppleScript's text item delimiters to astid
set NameParts to nameList

--> {{"012506", "Test1", "Group13", "001.jpg"}, {"012506", "Test14", "Group2", "050.jpg"}, {"020406", "Test2", "Group1", "061.jpg"}, {"020406", "Test20", "Group1", "071.jpg"}}

-- to get the filenames back:

set astid to AppleScript's text item delimiters
set AppleScript's text item delimiters to "_"
repeat with afile in NameParts
	set contents of afile to contents of afile as text
end repeat
set AppleScript's text item delimiters to astid
set namesAgain to NameParts
-- this can be avoided if at the beginning you: copy nameList to origList

So now nameList has four items, each of which is a list of four items; the parts of the filename from the original list. A loop that cycles through the four major items containing a loop that identifies the parts and moves files appropriately will do it.

Thanks Adam,

I have a script that creates subfolders and then subfolders by using the first two characters of the file name for the first subfolder and then another subsubfolder using the first four characters of the file name. I want to do something similar, but this time I want the characters before the first underscore to create a subfolder and then in this folder another subsubfolder using the characters between the first and second underscore and so on for the last folder but using the characters between the second and third underscore. All of these files will have different dates, groups and tests, so the character number will vary. The script should be smart enough to pickup the characters between the underscores.

I was trying to modify the script but no success, since I don’t really know ho to create folders or duplicate them.
Here is the script I’m using.

on adding folder items to this_folder after receiving These_items
	tell application "Finder"
		repeat with i in These_items
			tell (get name of i) to set {t_2, t_4} to {text 1 thru 2, text 1 thru 4}
			tell folder t_2 of folder this_folder
				if not (exists) then make folder at this_folder with properties {name:t_2}
				if not (exists folder t_4) then make folder at it with properties {name:t_4}
				move i to folder t_4
			end tell
		end repeat
	end tell
end adding folder items to

And here’s a slightly different approach that uses the ability of a mkdir shell script to create an entire folder chain in one go:

on adding folder items to this_folder after receiving These_items
	set mkdir_start to "mkdir -p '" & POSIX path of this_folder
	set astid to AppleScript's text item delimiters
	
	repeat with This_item in These_items
		set {folder:isFolder, package folder:isBundle, name:this_name} to (info for This_item)
		-- Creating new folders in this folder will retrigger the folder action.
		-- Exclude them, and any files whose names don't contain "_", from the process.
		if (not ((isFolder) or (isBundle))) and (this_name contains "_") then
			-- Get a list of the parts of This_item's name
			set AppleScript's text item delimiters to "_"
			set part_list to this_name's text items
			-- Replace the last part with an "empty text item".
			set item -1 of part_list to ""
			-- Create a folder chain in this_folder, subfolders named after file name parts 1 thru 3.
			set AppleScript's text item delimiters to "/"
			do shell script (mkdir_start & part_list & "'")
			-- Move the file into the "date" folder
			tell application "Finder" to move This_item to folder (item 1 of part_list) of this_folder
		end if
	end repeat
	
	set AppleScript's text item delimiters to astid
end adding folder items to

Thanks guys,
I’ll will try them out tomorrow and I’ll let you guys know how it goes.

MacAttack4

Sorry. I’ve just noticed you wanted the files to go into the “date” folders, not the innermost ones. I’ve edited my offering above accordingly.