Copying files to a folder until it reaches a certain size

Is there a way to add items to a folder until it reaches a certain size?

I was trying to figure out the following via Automator, but I think I need to write an AppleScript instead:

I have a folder named Unsorted with lots of subfolders. Inside these subfolders are MP3’s.

I want to select as many of these folders as possible until they total 650mb (so they fit on CD) and then move them to another folder, i.e. MP3 Cd1. Then repeat with the remaining files in the Unsorted folder, with the destination folder being MP3 Cd2 (and 3 etc) until the Unsorted folder is empty.

Any tips on how to accomplish this would be most appreciated.

Model: iMac
Browser: Safari 525.13
Operating System: Mac OS X (10.5)

This is modeled after a script by Jacques:

property DVD : 4.3E+9
property CD : 6.27 + 8
property sourceFolder : missing value

if button returned of (display dialog "Size for DVD or CD-ROM?" buttons {"DVD", "CD"}) is "DVD" then
	set maxFolderSize to DVD
else
	set maxFolderSize to CD
end if
set sourceFolder to (choose folder with prompt "Pick the folder you want to divide into DVD-sized folders:")
tell application "Finder" to set filesToMove to physical size of document files of sourceFolder -- get all size
set {currentSize, reStart, totalFileCount} to {0, 0, count filesToMove}
repeat with i from 1 to totalFileCount
	set currentSize to currentSize + (item i of filesToMove)
	if currentSize > maxFolderSize then
		moveFiles(i - 1 - reStart)
		set {currentSize, reStart} to {(item i of filesToMove), i - 1}
	end if
end repeat
if reStart is not totalFileCount then moveFiles(-1) -- move the remainder of the files -- the added line
display dialog "Finished!" buttons {"OK"} default button 1

on moveFiles(n)
	set currentFolderNum to 1
	tell application "Finder" to tell folder sourceFolder
		set folderName to text 1 thru 7 of (get name of document file 1)
		repeat while (exists item (folderName & " " & currentFolderNum))
			set currentFolderNum to currentFolderNum + 1
		end repeat
		
		set destFolder to make new folder at it with properties {name:folderName & " " & currentFolderNum}
		move (document files 1 thru n) to destFolder
	end tell
end moveFiles

Many thanks for the quick reply. Your script is very useful for moving files to CD (or DVD) sized folders. I was looking to move all subfolders of a particular directory, as opposed to the files within them, to a folder (up to the CD size limit), so they would then by organised by folder name for my MP3 CD player. I’ve achieved it via the following code. All comments and suggestions welcome - this is my first AppleScript and I’ve never been a coder anyway, so it can doubtless be improved (edit - just thought, I should change the code to check if the destination folder already exists).

– outcomment lines by adding (* at beginning and *) at the end

set folderSelected to choose folder “Select a folder”

tell application “Finder”
set folder_num to 1
set maxFolderSize to 680
set listOfFolders to every folder of folderSelected
make new folder at “Macintosh HD:Users:Darrell:Music” with properties {name:folder_num}
repeat with aFolder in listOfFolders
–used for debugging, so outcommented
(* display dialog "Found a folder named " & (name of aFolder) )
set destination_folder to folder folder_num of folder “Music” of folder “Darrell” of folder “Users” of startup disk
–used for debugging, so outcommented
(
display dialog "Destination folder is " & destination_folder )
set Posix_DestinationPath to quoted form of POSIX path of (destination_folder as text)
set the_destinationscript to "du -ks " & Posix_DestinationPath & “| awk ‘{print $1}’”
set the_destinationsize to do shell script the_destinationscript
–used for debugging, so outcommented
(
display dialog “The destination folder is " & the_destinationsize / 1024 & " MB in size” )
set Posix_SourcePath to quoted form of POSIX path of (aFolder as text)
set the_sourcescript to "du -ks " & Posix_SourcePath & “| awk ‘{print $1}’”
set the_sourcesize to do shell script the_sourcescript
–used for debugging, so outcommented
(
display dialog “The source folder is " & the_sourcesize / 1024 & " MB in size” )
if (the_sourcesize + the_destinationsize) / 1024 > maxFolderSize then
–used for debugging, so outcommented
(
display dialog "Total size is " & (the_sourcesize + the_destinationsize) / 1024 )
(
display dialog “Destination folder full” *)
set folder_num to folder_num + 1
make new folder at “Macintosh HD:Users:Darrell:Music” with properties {name:folder_num}
set destination_folder to folder folder_num of folder “Music” of folder “Darrell” of folder “Users” of startup disk
move aFolder to destination_folder
else
move aFolder to destination_folder
end if
end repeat
end tell