Archiving nested folders

I have this script which archives nested folders. I try to run script but get a apple event error. Also it seems to create mutiple folders with the some name. Any input would help. thanks.

script VariationOne
set subfolder_prefix to "subfolder "
set max_size to (640 * (1024 ^ 2)) --640 MBs
set {i, j, tempList} to {0, 1, {}}
set the_folder to (choose folder) as alias

tell application "Finder"
	get a reference to items of folder the_folder
	{result as list, result's physical size as list}
	set {itemList, sizeList} to result
	set new_folder to make new folder at the_folder �
		with properties {name:(subfolder_prefix & (j as string))}
	
	repeat with theIdx from 1 to itemList's length
		set the_size to sizeList's item theIdx
		if i + the_size > max_size then
			move tempList to new_folder
			set {i, j, tempList} to {0, j + 1, {}}
			set new_folder to make new folder at the_folder �
				with properties {name:(subfolder_prefix & (j as string))}
		end if
		set end of tempList to itemList's item theIdx
		set i to i + the_size
	end repeat
	move tempList to new_folder
end tell

display dialog ((itemList's length as string) & �
	" items split into " & (j as string) & " subfolders.") �
	buttons {"OK"} default button 1 with icon 1 giving up after 10

end script

Hi, Mike.

I can’t see why you have the code in a script object. Is it part of a larger script?

After I remove the script object wrapper, it seems to work OK on my (OS 8.6) machine - assuming that the idea is to move the contents of a folder into specially created subfolders in that same folder. I haven’t been able to test it with large folder sizes, but reduced max_size to simulate the effect.

I didn’t get any event errors or multiple folders with the same name. However, if the first item to be tested after the creation of a new folder is larger than max_size, an extra folder is created. This is because an empty list is saved to the first folder before the large item is added to the list. This should be easy to fix. Maybe it’ll incidentally fix your other multiple-folder problem…?

Sorry I couldn’t be more help.