Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Move files to a folder but one is not moving.

Hi, so this little script must have a schoolboy error in it as it works but leaves one behind.


It should move all the files from one folder to another.

Where am I going wrong, please?



property this_folder : (((path to home folder) as text) & "Desktop:TESTFOLDER:") as alias

property destinationFolder : (((path to home folder) as text) & "Desktop:Other:") as alias


tell application "Finder" to set itemList to every file in this_folder


tell application "Finder"

repeat with this_item in entire contents of this_folder

set filePath to this_item

do shell script "mv -n " & quoted form of (POSIX path of (filePath as alias)) & " " & quoted form of (POSIX path of (destinationFolder as alias))

end repeat

end tell

Posted on Nov 9, 2021 1:24 AM

Reply
Question marked as Best reply

Posted on Nov 9, 2021 10:56 AM

There's your clue right there.


If it helps, I think this problem is about a 9.4 on the scale of the 'what the heck is going on with my AppleScript' scale. It's subtle.


Conceptually, this comes down to the fact you're targeting a moving list.


Consider the line:


	repeat with this_item in entire contents of this_folder


Internally, this_item is a a counter that increments on each iteration of the loop - first iteration is 1, second is 2, etc.

Additionally entire contents of this_folder is not an object (or a list), it is command.


The line is more clearly seen as:


	repeat with this_item in (get entire contents of this_folder)


Why is that significant?


Well, let's assume there are 10 items in the folder.


On the first iteration, this_item is 1, and AppleScript goes off and gets entire contents of this_folder (10 items), and it extracts the first item. All well and good.


On the second iteration, this_item is 2. It goes off again and finds the entire contents of this_folder. However, this time there are only 9 items in the folder because you already moved one out. The script doesn't think anything of that, and it simple moves the second item from this list of 9 files.


On the third iteration, this_item is 3, and entire contents of this_folder is a list of 8 items, so it skips the first two


and so on.


So the fault is that you recalculate what 'entire items of' the folder is on each iteration, leaving half the files behind.


The fix is simple - put the list of files into a list, and iterate over that list, rather than recalculating the list every time (the script will also be much faster since it only calculates the list of files one time):


tell application "Finder"
	set theListOfFilesToMove to (entire contents of this_folder)
	repeat with this_item in theListOfFilesToMove
    ...


In this way you get the list of files, store then in a variable and iterate over that. Problem solved.

Similar questions

5 replies
Question marked as Best reply

Nov 9, 2021 10:56 AM in response to MattJayC

There's your clue right there.


If it helps, I think this problem is about a 9.4 on the scale of the 'what the heck is going on with my AppleScript' scale. It's subtle.


Conceptually, this comes down to the fact you're targeting a moving list.


Consider the line:


	repeat with this_item in entire contents of this_folder


Internally, this_item is a a counter that increments on each iteration of the loop - first iteration is 1, second is 2, etc.

Additionally entire contents of this_folder is not an object (or a list), it is command.


The line is more clearly seen as:


	repeat with this_item in (get entire contents of this_folder)


Why is that significant?


Well, let's assume there are 10 items in the folder.


On the first iteration, this_item is 1, and AppleScript goes off and gets entire contents of this_folder (10 items), and it extracts the first item. All well and good.


On the second iteration, this_item is 2. It goes off again and finds the entire contents of this_folder. However, this time there are only 9 items in the folder because you already moved one out. The script doesn't think anything of that, and it simple moves the second item from this list of 9 files.


On the third iteration, this_item is 3, and entire contents of this_folder is a list of 8 items, so it skips the first two


and so on.


So the fault is that you recalculate what 'entire items of' the folder is on each iteration, leaving half the files behind.


The fix is simple - put the list of files into a list, and iterate over that list, rather than recalculating the list every time (the script will also be much faster since it only calculates the list of files one time):


tell application "Finder"
	set theListOfFilesToMove to (entire contents of this_folder)
	repeat with this_item in theListOfFilesToMove
    ...


In this way you get the list of files, store then in a variable and iterate over that. Problem solved.

Nov 12, 2021 5:22 AM in response to Camelot

Hi Many Thanks.


I understood the principle it would be looking for a file that was there just couldn't figure out how to get round it.

Many Thanks


I used the shell script as I took it from an old script not sure why I used it. It works to move them individually.


I think Shell was used to move it from an internal Volume to an external drive. If i remember rightly applescript would only copy it to an external drive. Does that sound correct?

Matt

Move files to a folder but one is not moving.

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.