dividing files between folders

hey guys

I’m trying to divide up files. This is what I have so far


tell application "Finder"
	set the_folder to (choose folder)
	set theCos to (the_folder & "CaptureOne:Settings50:" as string) as alias
	set Num to count of ((files of (contents of (the_folder))))
	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2")
	set theDivide to Num / theNodes
	repeat with i from 1 to theNodes
		set the_NodeFolder to make new folder at the_folder with properties {name:"node" & (text -3 through -1 of ("00" & i))}
		set CapOne to make new folder at the_NodeFolder with properties {name:"CaptureOne"}
		set Settings to make new folder at CapOne with properties {name:"Settings50"}
	end repeat
	repeat theDivide times
		move file 1 of the_folder to folder "node001" of the_folder
		move file 1 of theCos to (the_folder & "node001:CaptureOne:Settings50:" as string) as alias
	end repeat
	repeat theDivide times
		move file 1 of the_folder to folder "node002" of the_folder
		move file 1 of theCos to (the_folder & "node002:CaptureOne:Settings50:" as string) as alias
	end repeat
	
end tell

I cant work out how to repeat theDivide call by the number node folders. the above code works for 2 nodes and even number of files to move. But I need to make t more dynamic.
Also the second move in the theDivide call is for a file that is associated with the other move, so if that if one file is missing it will screw the next file that is moved. Any hints on how to separate that out?
I use this code in another script I use


set file_cos_path to (the_folder & "CaptureOne:Settings50:" & file_name & ".cos" as string) as alias

To clarify:
You have a folder with sets of files. The first of each set should go into folder A, the second one into folder B, and so on. But sometimes a member of a set is missing.

Is it possible to select, say, the 2nd member of all sets by (part of) its name?

I think I should make it a little clearer :stuck_out_tongue: My fault trying to ram too much in.
I was hoping to count the files get the number of nodes a user has and divide the files amongst the nodes. This introduces the problem of left over files. So it may be better to set the number of nodes and then move file1 and file1a to the first node, file2 and file2a to the second node, file3 and file3a to the first node ( if two nodes where selected) etc etc. Does that make more sense ?

This is getting closer


tell application "Finder"
	set the_folder to (choose folder)
	--display dialog the_list as text
	set theCos to (the_folder & "CaptureOne:Settings50:" as string) as alias
	set Num to count of ((files of (contents of (the_folder))))
	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2")
	set theDivide to Num / theNodes
	repeat with i from 1 to theDivide
		set the_NodeFolder to make new folder at the_folder with properties {name:"node" & (text -3 through -1 of ("00" & i))}
		
		if not (exists folder "CaptureOne" of the_NodeFolder) then make new folder at the_NodeFolder with properties {name:"CaptureOne"}
		set Nodetext to the_NodeFolder as text
		--display dialog Nodetext
		set CapOne to (Nodetext & "CaptureOne" as string) as alias
		if not (exists folder "Settings50" of CapOne) then make new folder at CapOne with properties {name:"Settings50"}
		set Settings to (CapOne & "Settings50" as string) as alias
		set the_list to every file of the_folder as list
		set cos_list to every file of theCos as list
		move item 1 of the_list to the_NodeFolder
		move item 1 of cos_list to Settings
	end repeat
	
end tell

Ah.
So you make a number of “node” folders, and then move pairs of files into those folders.
When there are more pairs than folders you want to start again with the first folder?

File names, extensions, types do not matter? In what way is the 1st file different from the 2nd? Is that difference the same for all pairs?

set the_list to every file of the_folder as list

The ‘as list’ is not needed, you get a list anyway.

Making the folders can be shortened:

if not (exists folder "CaptureOne" of the_NodeFolder) then set CapOne to make new folder at the_NodeFolder with properties {name:"CaptureOne"}
if not (exists folder "Settings50" of CapOne) then set Settings to make new folder at CapOne with properties {name:"Settings50"}

Awesome! All the moves are sorted now for a single loop. So if user selects to make two nodes them node001 and node002 is created and file1.jpg is moved to node001 and file1.jpg.cos is moved to settings folder in node001. same happens to file2.jpg and file2.jpg.cos into node002. How can I get it to repeat while there are files in to top folder?

I think this might do it. Haven’t tested, though… And it will fail when the file lists are of unequal length.

tell application "Finder"
	set theFolder to (choose folder)
	set theCosFolder to (theFolder & "CaptureOne:Settings50:" as string) as alias
	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2")
	
	-- first make the folders
	-- if you leave out the leading zeroes you can get that number back much easier, later on
	-- so name them "node i", not "node00i"
	repeat with i from 1 to theNodes
		set theNodeFolder to make new folder at theFolder with properties {name:"node " & i as text} -- mind the space!
		set CapOneFolder to make new folder at theNodeFolder with properties {name:"CaptureOne"}
		set SettingsFolder to make new folder at CapOneFolder with properties {name:"Settings50"}
	end repeat
	
	-- list all files
	set the_list to every file of theFolder
	set cos_list to every file of theCosFolder
	set numberOfFiles to count the_list
	
	set j to 0 -- counter for node folders
	
	repeat with i from 1 to numberOfFiles
		set j to j + 1
		-- get node folder with number j
		-- get contained settings folder
		
		move item i of the_list to node_folder_j
		move item i of the cos_list to settings_folder_j
		if j = theNodes then set j to 0 -- start over
	end repeat
	
end tell

I’ve left finding “node folder j” for you :smiley:

This is what I have now and it work great for the first round


tell application "Finder"
	set the_folder to (choose folder)
	set theCos to (the_folder & "CaptureOne:Settings50:" as string) as alias
	set Num to count of ((files of (contents of (the_folder))))
	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2")
	
	repeat with i from 1 to theNodes
		set the_NodeFolder to make new folder at the_folder with properties {name:"node_" & i as text}
		if not (exists folder "CaptureOne" of the_NodeFolder) then set CapOne to make new folder at the_NodeFolder with properties {name:"CaptureOne"}
		if not (exists folder "Settings50" of CapOne) then set Settings to make new folder at CapOne with properties {name:"Settings50"}
		set the_list to every file of the_folder
		set cos_list to every file of theCos
		set Item1 to move item 1 of the_list to the_NodeFolder
		set Item1_name to name of Item1
		set cos_file to (the_folder & "CaptureOne:Settings50:" & Item1_name & ".cos" as string)
		move cos_file to Settings
	end repeat
	set targetFolder to folders of the_folder's contents as alias list
	set MasterList1 to targetFolder as list
	set itemlist to items 2 thru end of MasterList1
	set node1 to item 1 of itemlist
	set node2 to item 2 of itemlist
	repeat while the_folder contains files
		set Item1 to move item 1 of itemlist to the_NodeFolder
		set Item1_name to name of Item1
		set cos_file to (the_folder & "CaptureOne:Settings50:" & Item1_name & ".cos" as string)
		move cos_file to Settings
	end repeat
end tell

Is there another way I could do the repeat so I can set the node results???

Did you try the script I posted?

I don’t understand “set the node results”.

Yeah I did but cant work out how to set node_folder_j and settings_folder_j
I’m thinking it needs to use ASTID’s to find the node folders but I’m not sure

No TIDs at all.
You already know how to do this - it’s higher up in the script.
Here is a commented excerpt

tell application "Finder"
	
	(*	 this is your top folder	*)
	set theFolder to (choose folder)
	
	(*	this is the nested folder where item 2 of each pair sits	*)
	set theCosFolder to (theFolder & "CaptureOne:Settings50:" as string) as alias
	
	(* node folders are in theFolder	*)
	set theNodeFolder to make new folder at theFolder with properties {name:"node " & i as text}
	
	(* they get the same nested folder that theFolder has *)
	set CapOneFolder to make new folder at theNodeFolder with properties {name:"CaptureOne"}
	set SettingsFolder to make new folder at CapOneFolder with properties {name:"Settings50"}
	
	
	set j to 0 -- counter for node folders
	
	repeat with i from 1 to numberOfFiles
		set j to j + 1
		-- get node folder with number j
		(*	 it's named "node j" *)
		set thisNodeName to "node " & j as text
		(* it's in theFolder *)
		set thisNodeFolder to (theFolder as text & thisNodeName &":")  as alias
		
		(*	this is similar	*)
		-- get contained settings folder
		
		move item i of the_list to thisNodeFolder
		move item i of the cos_list to thisSettingsFolder
		if j = theNodes then set j to 0 -- start over
	end repeat
	
end tell

Isnt you code trying to move the third file to node 3 when only 2 exist?


tell application "Finder"
	set theFolder to (choose folder)
	set theCosFolder to (theFolder & "CaptureOne:Settings50:" as string) as alias
	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2")
	
	-- first make the folders
	-- if you leave out the leading zeroes you can get that number back much easier, later on
	-- so name them "node i", not "node00i"
	repeat with i from 1 to theNodes
		set theNodeFolder to make new folder at theFolder with properties {name:"node " & i as text} -- mind the space!
		set CapOneFolder to make new folder at theNodeFolder with properties {name:"CaptureOne"}
		set SettingsFolder to make new folder at CapOneFolder with properties {name:"Settings50"}
		
	end repeat
	
	-- list all files
	set the_list to every file of theFolder
	set cos_list to every file of theCosFolder
	set numberOfFiles to count the_list
	
	set j to 0 -- counter for node folders
	
	repeat with i from 1 to numberOfFiles
		set j to j + 1
		-- get node folder with number j
		(*     it's named "node j" *)
		set thisNodeName to "node " & j as text
		(* it's in theFolder *)
		set thisNodeFolder to ((theFolder as text) & thisNodeName & ":" as string) --as alias
		
		(*    this is similar    *)
		-- get contained settings folder
		
		move item i of the_list to thisNodeFolder
		--move item i of the cos_list to thisSettingsFolder
		if j = theNodes then set j to 0 -- start over
	end repeat
end tell

Thanks so much for helping me with this

It shouldn’t. When j=2 it gets reset at the bottom of the loop, so the 3rd pair go into node 1.
When all pairs are moved the script simply ends, which is much harder to do with nested or chained repeats.

Only remaining problem is pairs where one file is missing.
The script cannot figure out which pair is incomplete, and fails when it comes to the end of the shorter list.
But:

if (count the_list) ≠ (count cos_list) then
	-- sound the alarm!
end if

Strange but

set theNudesNum to theNodes as number

or

	set theNodes to text returned of (display dialog "Number of processing nodes" default answer "2") as number

fixed it.

THANK YOU for your patience