Sorting files into folders

I am trying to create a script that will copy a list of files to a list of folders in sequence.
It is supposed to do something like this:

afile.jpg>>copies to>>afolder
bfile.jpg>>copies to>>bfolder


zfile.jpg>>copies to>>zfolder

In the script below, I don’t necessarily want to move the destination folder to a copied folder, but I couldn’t think of any other way to deal with the sequence. With this script I get an error that says it cannot get aFolder and lists the folders.

Thanks in advance.

set inputFolder to "Stick:Test:XML:"
set destFolder to "Stick:Test:Teams:"
set copiedFolder to "Stick:Test:Finished:"

tell application "Finder"
	set fileList to files of folder inputFolder
	set folderList to folders of folder destFolder
	repeat with aFile in fileList
		try
			set the_name to name of aFile
			set the_folder to aFolder in folderList
			duplicate aFile to folder the_folder
			move aFolder to folder copiedFolder
			
		on error err
			display dialog err
		end try
	end repeat
end tell

That’s because you haven’t set aFolder to anything, so it doesn’t exist.

I’m not clear what you’re trying to do here. Do you have 26 files, each with names beginning with different alphabetical characters, that have to go into folders whose names begin with the same characters? Or do you simply want to distribute a list of files to a list of folders in whatever order they happen to be found?

I was guessing, from looking at another script, that the variable aFile (or aFolder) simply meant the first folder in a list. I was looking for a way for the script to know to process the first file in the list.

I simply want to script to move the first file to the first foleder and the second file to the second folder and so on. The number of files to be moved would be determined by the number offiles in the folder. The number of files always matcheds the number of destination folders. I hope that answers the questions.

Thanks

set inputFolder to "PowerBook HD:Desktop Folder:XML:"
set destFolder to "PowerBook HD:Desktop Folder:Teams:"

tell application "Finder"
  set fileList to files of folder inputFolder
  set folderList to folders of folder destFolder
  repeat with i from 1 to (count fileList)
    duplicate item i of fileList to item i of folderList
  end repeat
end tell

This does strictly what you ask - it copies the first file to the first folder, and so on. The problem with it is that you can’t guarantee which file or folder the Finder thinks is the first, second, or umpteenth in either of the main folders. If you don’t care about that, then the above code should do the trick. But do ask again if you need more precision. :slight_smile:

Thank you for that piece. It answers a lot of questions. We do need precision in the move as each file (file a) needs to correspond with (folder a) and (file b) needs to correspond with (folder b) and so on. That is the reason for moving the folder to another location so that the script would always move the first file to the first folder. Now that I’m writing this it occurs to me that each file would need to be moved rather than duplicated in order to make this happen.

Assuming, since you’ve asked on this forum, that you’re not using Mac OS X, you might try using the Finder’s ‘sort’ command to arrange the two lists according to your criterion for order. For instance, if you were going by alphabetical order, you could set the lists like this:

tell application "Finder"
  set fileList to (sort files of folder inputFolder by name)
  set folderList to (sort folders of folder destFolder by name)
  -- etc.

If the modification date were your criterion, you’d replace ‘by name’ with ‘by modification date’.

Unfortunately, ‘sort’ is still not working in Mac OS X. But if you were matching file to folder by a single letter of the alphabet, you could try the following. It’s rather sluggish on my 9.2.2 system, but pretty fast on the same machine running 10.2.8:

set inputFolder to (path to desktop as string) & "XML:"
set destFolder to (path to desktop as string) & "Teams:"

tell application "Finder"
  repeat with thisLetter in "abcdefghijklmnopqrstuvwxyz"
    try
      duplicate (the first file of folder inputFolder whose name begins with thisLetter) to (the first folder of folder destFolder whose name begins with thisLetter)
    end try
  end repeat
end tell