An easy one..

So all I need is a drop folder into which every file I drop has a file name beginning with 5 digits. They will be between 10000 and 13999. I have smaller archive folders to hold these in groups of 1000, IE a folder for 10000s, 11000s, 12000s, 13000s. How can I use Applescript to read the first 5 digits of the file name and move the file to it’s corresponding archive folder ?

Example file name

“10156_390_videofile_120707.mov”

Destination folder

“10000s”

Hi,
Try this:

property source_folder : (path to desktop as string) & "sort_folder" as alias --> create these folder structures on your desktop
property folder_10000 : (path to desktop as string) & "sort_folder:10000" as alias
property folder_11000 : (path to desktop as string) & "sort_folder:11000" as alias
property folder_12000 : (path to desktop as string) & "sort_folder:12000" as alias
property folder_13000 : (path to desktop as string) & "sort_folder:13000" as alias

on idle
	tell application "Finder"
		set item_list to every file of source_folder
		repeat with this_item in item_list
			set filename to the name of this_item
			set the_number to text 1 thru 5 of filename as number
			if the_number > 9999 and the_number < 11000 then
				move this_item to folder_10000
			else if the_number > 10999 and the_number < 12000 then
				move this_item to folder_11000
			else if the_number > 11999 and the_number < 13000 then
				move this_item to folder_12000
			else if the_number > 12999 and the_number < 14000 then
				move this_item to folder_13000
			end if
		end repeat
	end tell
end idle

save this as a Stay open application script and it should do what you require!
Thanks,
Nik

short version as droplet


property source_folder : (path to desktop as string) & "sort_folder:" --> the 1x000s folders are in the source_folder

on open these_items
	repeat with this_item in these_items
		tell application "Finder" to move this_item to folder (text 1 thru 2 of (get name of this_item) & "000s") of folder source_folder
	end repeat
end open

Looks great, but for some reason it isn’t working. I made a folder on the desktop named “sort_folder” and 4 folders inside it, 10000s - 13000s. Not sre what’s up. I’m sure it’s my fault.

Hi,
if you’re using Stefan’s code then you’ll need to save the script as an application and then drop your files on the application icon.
Nik

Nervermind! I think i got it. Thanks a lot guys!

Here’s my final script. I added some code to count how many files were sorted successfully. the “Uncarted” files are ones that haven’t been assigned a 5-digit code yet.

on open these_items
	set sort_success to 0
	set sort_fail to 0
	
	repeat with this_item in these_items
		tell application "Finder"
			set file_digits to (text 1 thru 5 of (get name of this_item))
			if (file_digits is greater than or equal to 10000) and (file_digits is less than or equal to 13999) then
				
				move this_item to folder (text 1 thru 2 of (get name of this_item) & "000s") of folder "Commercial Library" of folder "RAID Storage 700"
				set sort_success to sort_success + 1
			else
				move this_item to folder "Uncarted" of folder "Sort Me" of folder "Commercial Library" of folder "RAID Storage 700"
				set sort_fail to sort_fail + 1
			end if
		end tell
	end repeat
	set timer to 15
	
	display dialog "FINISHED!  Successful: " & sort_success & " Failed: " & sort_fail & "      This window will close in " & 15 & " seconds" with icon caution buttons {"OK"} giving up after 15
	
end open

It’s not a big deal, but anyone know how I can make the “15” in the dialog box count down?

Hi,

this doesn’t work reliably, because you’re comparing text (file_digits) with a number,
I recommend to coerce the text to a number

 set file_digits to (text 1 thru 5 of (get name of this_item)) as number

I had that in there at some point. The problem was for files that aren’t carted, the 5th character is sometimes an underscore, like this 1845_. That gives the error “Can’t make xxxxx into type number.” How can I tell the script to move to the else when that error is received?

EDIT

I think I got it! How does this look? It seems to work correctly.


on open these_items
	set sort_success to 0
	set sort_fail to 0
	
	repeat with this_item in these_items
		tell application "Finder"
			
			set file_digits to (text 1 thru 5 of (get name of this_item))
			try
				file_digits as number
				if (file_digits is greater than or equal to 10000) and (file_digits is less than or equal to 13999) then
					
					move this_item to folder (text 1 thru 2 of (get name of this_item) & "000s") of folder "Commercial Library" of folder "RAID Storage 700"
					set sort_success to sort_success + 1
				else
					move this_item to folder "Uncarted" of folder "Sort Me" of folder "Commercial Library" of folder "RAID Storage 700"
					set sort_fail to sort_fail + 1
				end if
				
			on error
				move this_item to folder "Uncarted" of folder "Sort Me" of folder "Commercial Library" of folder "RAID Storage 700"
				set sort_fail to sort_fail + 1
			end try
			
		end tell
	end repeat
	
	
	display dialog "FINISHED!  Successful: " & sort_success & " Failed: " & sort_fail & "      This window will close in " & 15 & " seconds" with icon caution buttons {"OK"} giving up after 15
	
end open