Need help w/ "Move image files into numbered folders, three deep"

Hello, please forgive me, I am new here.

What I have: Thousands of image files, named with item numbers (i.e. 12345.psd or 54321.eps)

My goal: To move images in a folder to another server, and distribute the files into numbered folders, three deep (meaning if the image is named “12345.psd”, it will go into “HI-RES IMAGES/1/12/123”. Image 54321.psd will go into "HI-RES IMAGES/5/54/543, and so on.

Images only need to go (up to) three folders deep. The many empty folders have all been created.

First, a big thanks to the other folks who have helped me get as far as I have. THANKS!
Now the code I have, which needs work (I’ll give you my issues later):


property baseFolder : missing value

on run
	set baseFolder to choose folder
end run

on open theFiles
	if baseFolder is missing value then set baseFolder to choose folder
	repeat with oneFile in theFiles
		set fileName to name of (info for oneFile)
		tell fileName to set destinationfolder to (baseFolder as text) & character 1 & ":" & text 1 thru 2 & ":" & text 1 thru 3 & ":"
		repeat
			try
				destinationfolder & fileName as alias
				display dialog "file " & quote & fileName & quote & " already exists." & return ¬
					& "Please choose a different name" default answer fileName buttons {"Cancel", "Overwrite", "OK"} default button 3
				set {fileName, button_returned} to {text returned of result, button returned of result}
				if (button_returned is "Overwrite") then
					tell application "Finder" to move oneFile to folder destinationfolder with replacing
					exit repeat -- File moved. Exit inner repeat.
				else
					tell application "Finder" to set name of oneFile to fileName -- File renamed. Do inner repeat to test new name.
				end if
			on error number -43 -- Catch the "File <name> wasn't found" error, but not "User Canceled".
				tell application "Finder" to move oneFile to folder destinationfolder
				exit repeat -- File move. Exit inner repeat.
			end try
		end repeat
	end repeat
end open


Thank you all! :slight_smile:

Model: 2 x 2.66 Ghz Dual-Core Intel Xeon, 4 GB 667 MHz DDR2 FB-DIMM
AppleScript: 1.10.7
Browser: Firefox 3.0.2
Operating System: Mac OS X (10.4)

Hi and welcome,

if you don’t mind about overwriting existing files, try this


property baseFolder : missing value

on run
	set baseFolder to choose folder with prompt "Choose base folder"
end run

on open theItems
	if baseFolder is missing value then set baseFolder to choose folder with prompt "Choose base folder"
	repeat with oneItem in theItems
		set Nm to name of (info for oneItem)
		tell Nm to set {f1, f2, f3} to {text 1, text 1 thru 2, text 1 thru 3}
		set destination to quoted form of (POSIX path of baseFolder & f1 & "/" & f2 & "/" & f3 & "/" & Nm)
		do shell script "/usr/bin/ditto " & quoted form of POSIX path of oneItem & " " & destination & " ;/bin/rm -r " & quoted form of POSIX path of oneItem
	end repeat
end open

Thanks StefanK! I
I usually want to overwrite them, but I’d like to have the option of having a dialogue come up and say “There already exists a file with this name, would you like to overwrite, rename or leave this file where it is?”. But I will try this out, and I think it will work great for now…let me see… am really hoping my boss will let me spend time learning more about Applescript. The four hours a day I have at home isn’t enough time! :slight_smile: I’ll get back to you asap…

Ooo, that’s nice! Works good. But it took a really long time to move 300 files. I will be moving thousands. Then again, this gives me the opportunity to head to Cafe Ladro for a Medici while my Mac chews on it… :wink:
Thanks again StefanK, this will get me by just fine. If you’re interested, here is a screenshot of the folder set-up I have: http://idisk.mac.com/fredgood/Public/ApplescriptGoal.png