Moving Images to different folders based on 1st number in name

OK. I’m stumped. I have 3 scripts to write and thought I would start with the easy one and I have crashed and burned. I have struggled with this script and really need some help. This script should move and organize several hundred images of a folder to several different folders based on the 1st number in the name. (i.e. sample name 12345_A.eps to be moved to a folder called “1”, sample name 62345_A.eps to be moved to a folder called “6”. You get the idea.) After moving and organizing the files, they should be deleted from the original folder. From the script I wrote, I get a window stating the work was done but nothing really happened. I’ll admit I’m not that good at scripting and would love any help anyone can provide.

Here is the script I wrote.
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::

property AceBankFolder : {}
property RRDftpFolder : {}
property theCount : {}
property theAnswer : {}
property getTextBefore : {}
property fileName : {}
property str : {}
property theMsg : {}



on run
	
	--      Locate the AceBank folder
	--
	if (AceBankFolder = {}) then
		set AceBankFolder to ¬
			choose folder with prompt "Select AceBank folder:"
		
		--      Locate the RRDftp folder
		--
		if (RRDftpFolder = {}) then
			set RRDftpFolder to ¬
				choose folder with prompt "Select RRDftp folder:"
			
			
			
			display dialog ¬
				"This script will move and organize the original images of the RRDftp folder to the AceBank. After moving and organizing the files in the RRDftp folder they will be deleted from the folder. Once Move Button is hit, there is no redo. " buttons {"Cancel", "Move"} default button 1 with icon note
			
			
			set theAnswer to the button returned of the result
		end if
	end if
	
	
	-- moving images -------------------------------------------
	--
	
	tell application "Finder"
		activate
		close every window
		make new Finder window
		set target of Finder window 1 to RRDftpFolder
		set current view of Finder window 1 to list view
		set bounds of Finder window 1 to {444, 51, 1003, 951}
		set position of Finder window 1 to {648, 47}
		
		
		
		set theCount to (count of every item in folder RRDftpFolder)
		if theCount = 0 then
			set theMsg to "Script completed." & return & "No items were moved."
		else if theCount = 1 then
			set theMsg to "Script completed." & return & "1 item was moved."
		else if theCount > 1 then
			set theMsg to "Script completed." & return & theCount & " items were moved."
			
		end if
		
		
		display dialog theMsg buttons {"OK"} default button "OK"
	end tell
	
	
	-- moving files to Ace Bank-------------------------------------------
	--
	
	set theCount to (count of every file in RRDftpFolder)
	if theCount > 1 then
		tell application "Finder"
			repeat with i from 1 to theCount
				set fileName to first character of name of eachitem
				
				
				
				--get fileName-------------------------------------------
				--
				
				if fileName starts with "1" then
					move eachitem to folder "1" of AceBankFolder with replacing
				else if fileName starts with "2" then
					move eachitem to folder "2" of AceBankFolder with replacing
				else if fileName starts with "3" then
					move eachitem to folder "3" of AceBankFolder with replacing
				else if fileName starts with "4" then
					move eachitem to folder "4" of AceBankFolder with replacing
				else if fileName starts with "5" then
					move eachitem to folder "5" of AceBankFolder with replacing
				else if fileName starts with "6" then
					move eachitem to folder "6" of AceBankFolder with replacing
				else if fileName starts with "7" then
					move eachitem to folder "7" of AceBankFolder with replacing
				else if fileName starts with "8" then
					move eachitem to folder "8" of AceBankFolder with replacing
				else if fileName starts with "9" then
					move eachitem to folder "9" of AceBankFolder with replacing
					
				end if
			end repeat
		end tell
		return theCount
		
		error -- error checking
		set str to "There has been an error!"
		
		quit
	end if
	

	
	-- resetting information
	set AceBankFolder to {}
	set RRDftpFolder to {}
	set theCount to {}
	set theAnswer to {}
	set getTextBefore to {}
	set fileName to {}
	set str to {}
	set theMsg to {}
	
end run

::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::
::::::::::::::::::::::::::::::::::::::::::::::::

Any help would be greatly appreciated.
Thanks guys

AppleScript: AppleScript 1.10.7
Browser: Safari 419.3
Operating System: Mac OS X (10.5)

Hi,

welcome to MacScripter :slight_smile:

try this:


property AceBankFolder : missing value
property RRDftpFolder : missing value

--      Locate the AceBank folder
if AceBankFolder = missing value then set AceBankFolder to choose folder with prompt "Select AceBank folder:"

--      Locate the RRDftp folder--
if RRDftpFolder = missing value then set RRDftpFolder to choose folder with prompt "Select RRDftp folder:"

display dialog "This script will move and organize the original images of the RRDftp folder to the AceBank. After moving and organizing the files in the RRDftp folder they will be deleted from the folder. Once Move Button is hit, there is no redo. " buttons {"Cancel", "Move"} default button 1 with icon note

-- saving the result is not needed. If the user presses Cancel, the script will be aborted

-- (set theAnswer to the button returned of the result)

-- moving files to Ace Bank-------------------------------------------
--
tell application "Finder" to set theFiles to (get files of RRDftpFolder)
set theCount to count theFiles
set x to 0 -- error counter
set POSIX_AceBankFolder to POSIX path of AceBankFolder
repeat with eachFile in theFiles
	tell application "Finder" to set fName to name of eachFile
	try
		-- the shell command moves the files to the destination location and delete them at the source location
		do shell script "mv " & quoted form of POSIX path of (eachFile as alias) & space & quoted form of (POSIX_AceBankFolder & character 1 of fName & "/" & fName)
	on error
		set x to x + 1
	end try
end repeat

set theMsg to "Script completed." & return
if theCount = 0 then
	set theMsg to theMsg & "No items were moved."
else if theCount = 1 then
	set theMsg to theMsg & "1 item was moved."
else if theCount > 1 then
	set theMsg to theMsg & theCount & " items were moved."
end if

display dialog theMsg & return & x & " error(s)" buttons {"OK"} default button "OK"

Thank You StefanK
That works fantastic!!! For “learning” sake, I see I had way to much stuff in my original script and wrong terminology. Just a few questions if you don’t mind.

  1. Is there a significance to using the term “POSIX” as in “POSIX_AceBankFolder”

  2. On the shell script section:

do shell script "mv " & quoted form of POSIX path of (eachFile as alias) & space & quoted form of (POSIX_AceBankFolder & character 1 of fName & “/” & fName)

I am confused. Is this defining the shell script “mv” or referring to a predifined script “mn” existing somewhere?

Thanks again
Mike

It’s not really wrong terminology, it’s just a different way.

of course, shell scripts expect POSIX (slash separated) paths, plain AppleScript HFS (colon separated) paths

the shell script executes the shell command move file /path/to/source to /path/to/destination with replacing and deleting file at source location

POSIX path is what’s important, not the variable naming.

Thanks for the lesson and saving my butt on this. You dah best.
Mike

I was working on this, and the got caught up on a Very, very long phone call,
So I thought I would post it any way.
It uses only applescript (even though I normally would us mv myself and I like the way Stefan has done it) , This iwas meant to be the first quick draft (phone call above…).And I wanted to keep as close to you script as poss as I suspect you were trying to use subroutines…

set AceBankFolder to {}
property Folder_Number : {"1", "2", "3", "4", "5", "6"}
set RRDftpFolder to {}
global RRDftpFolder, AceBankFolder
--      Locate the AceBank folder
--
if (AceBankFolder = {}) then
	set AceBankFolder to ¬
		choose folder with prompt "Select AceBankFolder folder:"
end if
if (RRDftpFolder = {}) then
	set RRDftpFolder to ¬
		choose folder with prompt "Select RRDftp folder:"
end if
display dialog "This script will move and organize the original images of the RRDftp folder to the AceBank folders . After moving and organizing the files in the RRDftp folder they will be deleted from the folder. Once Move Button is hit, there is no redo. " buttons {"Cancel", "Move"} default button 1 with icon note
if the button returned of the result is "Move" then
	check_files()
end if
on check_files()
	tell application "Finder"
		set theCount to (count of every item in folder RRDftpFolder)
		set theItems to (every item in folder RRDftpFolder) as list
		if theCount = 0 then
			set theMsg to "Script completed." & return & "No items were moved."
		else if theCount = 1 then
			set theMsg to "Script completed." & return & "1 item was moved."
			my setUp_windows()
			my move_files(theItems)
		else if theCount > 1 then
			
			set theMsg to "Script completed." & return & theCount & " items were moved."
			my setUp_windows()
			my move_files(theItems)
		end if
		display dialog theMsg buttons {"OK"} default button "OK"
	end tell
end check_files
on setUp_windows()
	tell application "Finder"
		activate
		close every window
		make new Finder window
		set target of Finder window 1 to RRDftpFolder
		set current view of Finder window 1 to list view
		set bounds of Finder window 1 to {444, 51, 1003, 951}
		set position of Finder window 1 to {648, 47}
	end tell
end setUp_windows
on move_files(theItems)
	tell application "Finder"
		repeat with i from 1 to number of items in theItems
			set this_item to item i of theItems
			set fileName to first character of (get displayed name of this_item)
			log fileName
			repeat with ii from 1 to number of items in Folder_Number
				set this_item_number to item ii of Folder_Number as string
				if fileName is equal to (ii as string) then
					tell application "Finder"
						move this_item to folder (ii as string) of AceBankFolder with replacing
					end tell
				end if
			end repeat
		end repeat
	end tell
end move_files

Thanks Mark for updating. I appreciate the input.
Mike