Problem with Checking for Folder

Hey guys,

I know that this has been discussed more times than I can count, but I cannot get this script to work for the life of me.

I have a list of files within one folder:
1234567890_A.jpg
1234567890_B.jpg
1234567890_C.jpg
1234555555_A.jpg
1234555555_B.jpg
1234555555_C.jpg
1234555555_D.jpg
1234567888_A.jpg
1234567888_B.jpg
…so on and so forth…

Anyway, I need to take these files, move them into a folder with the 10-digit number and a suffix - “1234567890 User Inserted Suffix Here”.

When the files are moved, I am trying to rename them at the same time, so I need the files being moved into the “1234567890 User Inserted Suffix Here” folder to be renamed like this:
1234567890_1_Text Here.jpg
1234567890_2_Text Here.jpg
1234567890_3_Text Here.jpg

Currently, I am grabbing each file within the parent folder and moving only the ones that begin with a 10 digit number (I need it to do it like this), and checking to see if a folder has been created for it yet. If it has, increment the counter and move the file into it’s subfolder with the new name - if it has not, reset the increment counter to 1, create a subfolder with the new name, and move the file into that folder.

My problem is that the incremental counter doesn’t seem to be incrementing. So the script is creating the new folders and everything correctly, and the correct files are being moved into the new folders, but the files within those new folders are being overwritten because the counter isn’t incrementing. I hope that makes sense… So:

1234567890_A.jpg is being renamed to 1234567890_1_Text Here and moved to “1234567890 User Inserted Suffix Here”
BUT
1234567890_B.jpg is being renamed to 1234567890_1_Text Here and moved to “1234567890 User Inserted Suffix Here” as well, thereby overwriting the previous file.

Here is the script that I have:


-- Set properties
property jpgMovedCount : 0
property rawMovedCount : 0
property renameCount : 1

-- Set Handlers
on coerceToInteger(theVariable)
	try
		-- Check if theVariable can be coerced to an integer
		theVariable as integer
		return true
	on error
		return false
	end try
end coerceToInteger

on folderExists(theFolder)
	try
		get theFolder as alias
		return true
	on error
		return false
	end try
end folderExists

-- Load Progress Bar
on InitializeProgressBar()
	return load script alias (((path to me) as string) & "Contents:Resources:Scripts:BP Progress Bar Controller.scpt")
end InitializeProgressBar

-- Prompt for which action to take
set userAction to button returned of (display dialog "Which files would you like to move?" with icon note buttons {"JPGs", "RAWs", "Both"} default button 3)

if userAction = "JPGs" then
	-- Set name of the event
	set brandSuffix to text returned of (display dialog "Enter the name of the event." buttons {"Cancel", "OK"} default button 2 default answer "Event Name")
	
	-- Choose source folder
	set jpgFolder to choose folder with prompt "Choose source folder containing JPGs."
	set jpgFolderPosPath to POSIX path of jpgFolder
	
	set progressBar to my InitializeProgressBar()
	tell progressBar to initialize("Folder Creator")
	
	tell progressBar
		setStatusTop to "Initializing & Collecting Images."
	end tell
	
	-- Find the JPGs
	tell application "Finder" to set jpgFiles to files of jpgFolder whose name extension is "jpg"
	
	set jpgFileCount to count (jpgFiles)
	
	tell progressBar to setMax to jpgFileCount
	
	-- If there are more than 0 files, run the script, otherwise stop it.
	if jpgFileCount > 0 then
		-- Begin moving the JPGs to their own SKU folders
		repeat with aFile in jpgFiles
			-- Get the name of the file
			set fileName to name of aFile
			-- Grab the first 10 digits of the file name
			set styleNum to text 1 thru 10 of fileName
			
			-- Check the first 10 digits of fileName to see if they're an integer
			if coerceToInteger(styleNum) = true then
				-- Increment jpgMovedCount
				set jpgMovedCount to jpgMovedCount + 1
				
				-- Add brandSuffix to prefix
				set prefix to styleNum & " " & brandSuffix
				
				-- Set path of folder to be created
				set newFolder to jpgFolderPosPath & prefix
				
				set checkFolder to POSIX path of newFolder
				
				-- Begin renaming function
				if folderExists(checkFolder) = false then
					-- Create new folder with prefix
					do shell script "mkdir -p " & quoted form of POSIX path of newFolder
					
					set renameCount to 1
					
					do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & styleNum & "_" & renameCount & "_NOCOLOR")
				else
					set renameCount to renameCount + 1
					
					do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & styleNum & "_" & renameCount & "_NOCOLOR")
				end if
				
				-- Set status within the progress bar and increment
				tell progressBar
					setStatusTop to "Creating Folders & Moving Images."
					setStatusBottom to "Processing " & fileName
					increase by 1
				end tell
			else
				-- Just increment the progress bar
				tell progressBar
					increase by 1
				end tell
			end if
		end repeat
		
		tell progressBar to quit
		
		-- Display completion dialog with jpgMovedCount
		if jpgMovedCount ≥ 150 then
			display dialog jpgMovedCount & " images have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
		else
			display dialog jpgMovedCount & " images have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
		end if
		
		-- Reset jpgMovedCount
		set jpgMovedCount to 0
	else
		-- Quit the progress bar
		tell progressBar to quit
		
		display dialog "There were 0 images to move! :(" with icon note buttons {"Done!"} default button 1 giving up after 2
	end if
...

Any help with this would be GREATLY appreciated!

Oh, and the reason I said it’s been discussed more times than I can count is because I feel like the issue lies with checking whether or not the folder exists…

Hi,

the method to check the folder expects HFS paths, because a POSIX path cannot be coerced directly to alias
Try


.
 if folderExists((jpgFolder as text) & prefix) = false then

And you, sir, are a genius! Thank you very much! Now my only problem is that there is an option to move RAW files, and in order to do that, my script needs to move the RAW files as well as the XMP files. With the script running the way it is (with your change), it works perfectly for just JPGs, but the XMPs wouldn’t be named exactly as their RAW counterparts. They would need to be like so:

1234567890_1_NOCOLOR.CR2
1234567890_1_NOCOLOR.xmp
1234567890_2_NOCOLOR.CR2
1234567890_2_NOCOLOR.xmp

How would I be able to do that?

Thanks again for fixing the JPG part. I just couldn’t figure it out and after looking at it for so long, I was starting to confuse myself, haha.

Here is the portion of the script I’m using to move all JPGs and RAW/XMPs. The JPGs folders are created in a different directory than the RAW/XMP folders. Also, the JPG folders get a suffix added, but the RAW/XMP folders are only named with the 10-digit number.


else if userAction = "Both" then
	-- Set name of the event
	set brandSuffix to text returned of (display dialog "Enter the name of the event." buttons {"Cancel", "OK"} default button 2 default answer "Event Name")
	
	-- Choose source JPG folder
	set jpgFolder to choose folder with prompt "Choose source folder containing JPGs."
	set jpgFolderPosPath to POSIX path of jpgFolder
	
	-- Choose source RAW folder
	set rawFolder to choose folder with prompt "Choose source folder containing RAWs."
	set rawFolderPosPath to POSIX path of rawFolder
	
	set progressBar to my InitializeProgressBar()
	tell progressBar to initialize("Folder Creator")
	
	tell progressBar
		setStatusTop to "Initializing & Collecting Images."
	end tell
	
	-- Find the JPGs
	tell application "Finder" to set jpgFiles to files of jpgFolder whose name extension is "jpg"
	-- Find the RAWs
	tell application "Finder" to set rawFiles to files of rawFolder whose name extension is "CR2"
	-- Find the XMPs
	tell application "Finder" to set xmpFiles to files of rawFolder whose name extension is "xmp"
	
	set jpgFileCount to count (jpgFiles)
	set rawFileCount to count (rawFiles)
	set xmpFileCount to count (xmpFiles)
	set totalFileCount to jpgFileCount + rawFileCount + xmpFileCount
	
	tell progressBar to setMax to totalFileCount
	
	-- If there are more than 0 files, run the script, otherwise stop it.
	if totalFileCount > 0 then
		-- Begin moving JPG files
		repeat with aFile in jpgFiles
			-- Get the name of the file
			set fileName to name of aFile
			-- Grab the first 10 digits of the file name
			set prefix to text 1 thru 10 of fileName
			
			-- Check the first 10 digits of fileName to see if they're an integer
			if coerceToInteger(prefix) = true then
				-- Increment jpgMovedCount
				set jpgMovedCount to jpgMovedCount + 1
				
				-- Add brandSuffix to prefix
				set prefix to prefix & " " & brandSuffix
				
				-- Set path of folder to be created
				set newFolder to jpgFolderPosPath & prefix
				
				-- Copy file to new folder with prefix name + brandSuffix
				do shell script "mkdir -p " & quoted form of POSIX path of newFolder
				-- Delete source JPG
				do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & fileName)
				
				-- Set status within the progress bar and increment
				tell progressBar
					setStatusTop to "Creating Folders & Moving Images."
					setStatusBottom to "Processing " & fileName
					increase by 1
				end tell
			else
				-- Just increment the progress bar
				tell progressBar
					increase by 1
				end tell
			end if
		end repeat
		
		-- Begin moving RAW files
		repeat with aFile in rawFiles
			-- Get the name of the file
			set fileName to name of aFile
			-- Grab the first 10 digits of the file name
			set prefix to text 1 thru 10 of fileName
			
			-- Check the first 10 digits of fileName to see if they're an integer
			if coerceToInteger(prefix) = true then
				-- Increment rawMovedCount
				set rawMovedCount to rawMovedCount + 1
				
				-- Set path of folder to be created
				set newFolder to rawFolderPosPath & prefix
				
				-- Copy file to new folder with prefix name + brandSuffix
				do shell script "mkdir -p " & quoted form of POSIX path of newFolder
				-- Move source file
				do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & fileName)
				
				-- Set status within the progress bar and increment
				tell progressBar
					setStatusTop to "Creating Folders & Moving Images."
					setStatusBottom to "Processing " & fileName
					increase by 1
				end tell
			else
				-- Just increment the progress bar
				tell progressBar
					increase by 1
				end tell
			end if
		end repeat
		
		-- Begin moving XMP files
		repeat with aFile in xmpFiles
			-- Get the name of the file
			set fileName to name of aFile
			-- Grab the first 10 digits of the file name
			set prefix to text 1 thru 10 of fileName
			
			-- Check the first 10 digits of fileName to see if they're an integer
			if coerceToInteger(prefix) = true then
				-- Set path of folder to be created
				set newFolder to rawFolderPosPath & prefix
				
				-- Copy file to new folder with prefix name + brandSuffix
				do shell script "mkdir -p " & quoted form of POSIX path of newFolder
				-- Delete source JPG
				do shell script "mv " & quoted form of POSIX path of (aFile as text) & space & quoted form of (newFolder & "/" & fileName)
				
				-- Set status within the progress bar and increment
				tell progressBar
					setStatusTop to "Cleaning up after myself..."
					setStatusBottom to "Processing " & fileName
					increase by 1
				end tell
			else
				-- Just increment the progress bar
				tell progressBar
					increase by 1
				end tell
			end if
		end repeat
		
		tell progressBar to quit
		
		set totalMovedCount to jpgMovedCount + rawMovedCount
		
		-- Display completion dialog with totalMovedCount
		if totalMovedCount ≥ 200 then
			display dialog "HOLY CRAP! I'm sweating after that one. " & return & return & jpgMovedCount & " JPGs have been moved!" & return & rawMovedCount & " RAWs have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
		else
			display dialog "Easy peezy lemon squeezy! " & return & return & jpgMovedCount & " JPGs have been moved!" & return & rawMovedCount & " RAWs have been moved!" with icon note buttons {"Done!"} default button 1 giving up after 2
		end if
		
		-- Reset Moved Counts
		set jpgMovedCount to 0
		set rawMovedCount to 0
	else
		-- Quit the progress bar
		tell progressBar to quit
		
		display dialog "There were 0 images to move! :(" with icon note buttons {"Done!"} default button 1 giving up after 2
	end if
end if
...