Move files into new folder -script stutters

Hey folks,

Im writing an AS that will organize bunches of files into appropriately named folders. We have tons of files we work with that have names like example_001.tif, example_002.tif --etc and the idea is to create a folder named “example” and put those files into it. example2_001.tif and example2_002.tif would go into a newly created “example2” folder and so on…The naming convention matters as the script ignores the last 8 characters of each file when creating the folder.

The script I have basically works, except that after it creates the folder and starts to move the files, it soon after hits this error: Finder got an error: Can’t get item 5 of alias “path:to:file:”. If you click OK, and rerun the script, it will continue to move files into the correct folder, but fail again shortly thereafter, do it again, it moves a few more and so on until it has moved all the files successfully.

All the files are the same type and Ive noticed that, given the same amount of files, it will always fail on the same item number. Its not the file itself though, if you change the files, but keep the same amount of files, and it still fails on that item #. I tried simplifying the script down to a more basic script which would move all files from a chosen folder into a hard coded destination folder, and that failed too. Not sure whats happening. Please help! Here is the full script, followed by my simplified script for testing. Thanks a lot for any help.


property sourceFolder : missing value
if sourceFolder = missing value then set sourceFolder to choose folder with prompt "Select folder to organize:"

tell application "Finder"
	set ASTID to AppleScript's text item delimiters
	set AppleScript's text item delimiters to ""
	
	repeat with i from 1 to number of files in folder sourceFolder
		set thisItem to item i of sourceFolder
		set fileName to name of thisItem
		
		set charCount to count of characters in fileName
		set x to 1
		repeat while character x of fileName is "_"
			set x to x + 1
			set currChar to character x of fileName
		end repeat
		set startCount to x
		set endCount to charCount - 8
		
		set folderName to characters startCount thru endCount of fileName as text
		if not (exists folder folderName of sourceFolder) then
			make folder at sourceFolder with properties {name:folderName}
		end if
		
		set posixFile to POSIX path of (thisItem as alias)
		set posixFolder to ((POSIX path of sourceFolder) & folderName)
		set theCommand to "mv " & quoted form of posixFile & space & quoted form of posixFolder
		try
			do shell script theCommand
		end try
	end repeat
	set ASTID to AppleScript's text item delimiters
end tell
if sourceFolder is not missing value then set sourceFolder to missing value
display dialog "The Organize Folders script has completed."

Simpler script for testing, same effect


property sourceFolder : missing value
if sourceFolder = missing value then set sourceFolder to choose folder with prompt "Select folder to organize:"

tell application "Finder"
	if not (exists folder "dest" in sourceFolder) then
		make new folder at sourceFolder with properties {name:"dest"}
	end if
	repeat with i from 1 to number of items in folder sourceFolder
		
		try
			set thisItem to item i of folder sourceFolder
		on error errormessage
			display dialog errormessage
		end try
		
		if kind of thisItem is not "folder" then
			set posixFile to POSIX path of (thisItem as alias)
			set posixFolder to ((POSIX path of sourceFolder) & "dest")
			set theCommand to "mv " & quoted form of posixFile & space & quoted form of posixFolder
			try
				do shell script theCommand
			on error errormessage
				display dialog errormessage
			end try
		end if
	end repeat
end tell
if sourceFolder is not missing value then set sourceFolder to missing value
display dialog "The Organize Folders script has completed."

Hi,

this is a common problem.
In this case the repeat form repeat with i from x to y can’t work.

Assume you have 5 files. After moving 3 files you try to retrieve item 4 of the folder, but there are only 2 left. :wink:
For this purpose use this form

tell application "Finder" to set theFiles to files of folder sourceFolder
repeat with thisItem in theFiles
.

because the list of files will be retrieved once

here is your script, it uses also a shell script to create the subfolders.
I guess, the name of the new folder is the portion of the file name before the underline character
The property is not needed, because you reset it to missing value at the end, so you can omit it


set sourceFolder to choose folder with prompt "Select folder to organize:"

tell application "Finder" to set theFiles to files of folder sourceFolder
repeat with thisItem in theFiles
	set fileName to name of (info for thisItem as alias)
	set folderName to text 1 thru ((offset of "_" in fileName) - 1) of fileName
	set newFolder to quoted form of (POSIX path of sourceFolder & folderName)
	do shell script "/bin/mkdir -p " & newFolder -- creates the folder if it doesn't exist
	do shell script "/bin/mv " & quoted form of POSIX path of (thisItem as alias) & space & newFolder
end repeat
display dialog "The Organize Folders script has completed."