parsing through 4000 files by name

I’m trying to dedupe a folder with a large number of files in it.

I found a script elsewhere on this site and modified it, not even bothering to rename the variables; and just commenting out unnecessary (for me) code. It works great for 25 or so files. But, I have a directory of nearly 4000 images. Luckily, the dupes are all renamed like 'originalfilename.jpg, originalfilename(1).jpg, originalfilename(2).jpg, etc. So, my logic is to 'move all files ending in ‘).jpg’

set TIFFolder to (choose folder with prompt "select folder with all files to parse.")
set myDestFolder to (choose folder with prompt "Where is the destination folder?")
tell application "Finder" to set TIFFiles to files of TIFFolder whose name ends with ").jpg"

--set {TID, text item delimiters} to {text item delimiters, space} --so I can deliminate between [shot name] and the [sequential counter]
with timeout of 300 seconds
	repeat with aFile in TIFFiles
		set fileName to name of aFile
		--	set shotName to first text item of fileName
		set sourcePath to quoted form of POSIX path of (aFile as text)
		set destinationPath to quoted form of (POSIX path of myDestFolder)
		do shell script "/usr/bin/ditto " & sourcePath & space & destinationPath & "; /bin/rm " & sourcePath
	end repeat
	--set text item delimiters to TID
end timeout

Problem is with this many files, the finder is crashing after about 90 seconds and the script is throwing a ‘AppleEvent times out -1712’. Anyone have any suggestions for another way of parsing this many files without killing the finder?

HI.
Sure the code crashs after 90 seconds and not after 300 seconds?
Remove with timeout of 300 seconds and end timeout code lines.The error relays on this code lines.


set TIFFolder to (choose folder with prompt "Select folder with all files to parse.")
set myDestFolder to (choose folder with prompt "Where is the destination folder?")

tell application "Finder"
	set TIFFiles to (files of TIFFolder whose name ends with ").jpg") as alias list
	repeat with aFile in TIFFiles
		move aFile to myDestFolder
	end repeat
end tell

I only added the timeout lines because it was crashing already. I thought maybe I could increase the timeout length with that function. But, it’s still crashing.

So, what’s the significance of your question about if it crashes at 90 seconds? am I missing something obvious?

An alternative is to use the find shell command to find the files to be moved and to use System Events to actually move the files. I don’t know how many files will actually be moved, so this may not work well either.

Please note that:

  • The maxdepth find option restricts the search to the top-level source folder only.
  • I’ve tested this script but please try this on a test folder to make sure it works correctly.

set sourceFolder to POSIX path of (choose folder with prompt "Select folder with all files to parse.")
set targetFolder to (choose folder with prompt "Where is the destination folder?")
set searchString to ").jpg"

do shell script "find " & quoted form of sourceFolder & " -iname *" & quoted form of searchString & " -maxdepth 1 -type f ; true"
set theFiles to paragraphs of result

display dialog "Move " & (count theFiles) & " files." default button "Cancel"

tell application "System Events"
	try
		move theFiles to targetFolder
	on error errorMessage
		display dialog "System Events reported this error:" & return & errorMessage buttons {"Cancel"} default button 1
	end try
end tell

TREMENDOUS! Thanks. That worked perfectly. Moved 1400 files in 21 seconds. I’ll be saving this to my little bag of scripts for the future.

Many thanks!

The timeout problem was in your do shell script code line. This is one reason to not use do shell script for processing huge amount of files. To work correct such do shell script command, you forced give to it proper timeout.

Hi.

Another possibility with ditto is to use a wildcard in the source path:

set TIFFolder to quoted form of POSIX path of (choose folder with prompt "select folder with all files to parse.")
set myDestFolder to quoted form of POSIX path of (choose folder with prompt "Where is the destination folder?")

do shell script "usr/bin/ditto " & TIFFolder & "*').jpg' " & myDestFolder

ok, small side question. I’m trying to save the fully working script for future use. So, I wanted to take user input for the search string.

set sourceFolder to POSIX path of (choose folder with prompt "Select folder with all files to parse.")
set targetFolder to (choose folder with prompt "Where is the destination folder?")
set searchString to display dialog ¬
	"What string shall I search for (from the right side of the file name)" default answer ").jpg"

do shell script "find " & quoted form of sourceFolder & " -iname *" & quoted form of searchString & " ; true"
set theFiles to paragraphs of result

display dialog "Move " & (count theFiles) & " files." default button "Cancel"

tell application "System Events"
	move theFiles to targetFolder
end tell

the only thing I changed is the ‘set searchString’ line to take user entry. But, it throws up a ‘can’t make quoted form in to unicode text’ error. I’ve tried using the ‘as text’ and ‘as unicode’ qualifiers at the end. Is this something simple, or should I just make a note to hard code in the searchString?

set searchString to text returned of (display dialog ¬
	"What string shall I search for (from the right side of the file name)" default answer ").jpg")

cudaboy_71. A display dialog with a default answer parameter returns both the button selected and text entered in the text field. That’s why KniazidisR’s suggestion is necessary.

I learn something new every time I try to script something. I’ve been using AppleScript since it first came out (though I’m not a dev by trade)…you’d think I’m old enough to have come across that before.

Thanks.

Hi. The OP’s file relocation appears to have already been accomplished, however, Nigel’s ditto wildcard suggestion will be faster than the Finder or System Events methods, and it also works with mv.

do shell script "mv " & my (choose folder with prompt "Select source.")'s POSIX path's quoted form & "*').jpg' " & my (choose folder with prompt "Select destination.")'s POSIX path's quoted form

Added that to my personal library of scripts as well.

Many thanks all around to everyone who contributed.

Nigel, any thoughts on why your script throws an error (“sh: usr/bin/ditto: No such file or directory”) here? Works OK if I use mv or cp instead.

Er. No. In a word. :confused: I copied the path to ditto from cudaboy_71’s original script and it works fine for me. Normally I’m quite lazy and just use the executable’s name rather than the path to it. My immediate guesses would be that either the ditto executable on your machine has been moved or removed at some point in the past or, since I imagine you’re running a pre-release version of Catalina, that the layout’s different on that system.

I do get a different error if the source folder doesn’t contain any items with names ending with the search string: “ditto: can’t get real path for source ‘/path/to/folder/*).jpg’”, which is something to bear in mind if using my suggestion. I did suddenly worry that the “*” wildcard might include path delimiters, with matching files from subfolders being copied too. But this doesn’t appear to be the case — in Mojave, at least.

No, this is under 10.14.6. I don’t think I’ve moved it. Strange, because I used ditto for something else not long ago.

Later: And now it’s working fine. Very strange glitch.