Compare folder contents then do something magical

Hi, I deleted my previous post - it was a bit rambly, but also because I may have thought of a simpler solution.
I thought it was simpler, but after some Googling I’ve come up with nothing. So I’ll just ask if anyone feels generous to point me in the right direction for a solution.

How could I compare two folder contents, and once those contents match ask applescript to do something?

eg a source folder has 10 files, once a destination folder matches the same number of files DO MAGIC?

I’m more than happy to send a donation for the right answer :smiley:

Hey, I’m not sure if this will solve your problem but it should do what you’ve asked in the post.


--Set folder paths
set folderA to "HDD:Users:username:Desktop:FolderA:" as alias
set folderB to "HDD:Users:username:Desktop:FolderB:" as alias


--Create lists of folder contents for each folder
tell application "System Events"
	set listA to get the name of every disk item of folderA
	set listB to get the name of every disk item of folderB
end tell


--Compare each item of folderA to items of folderB
repeat with i from 1 to the count of listA
	set theFile to (item i of listA)
	if theFile is in listB then
		log "Match found for file " & theFile
		set foundFile to true
	else
		log "No match found for file " & theFile
		set foundFile to false
	end if
end repeat


--Do magic..... or not
if foundFile is true then
	display dialog "Do Magic"
	--Magic goes here
else
	display dialog "ERROR! First mismatch at file:" & return & theFile
end if

@RBUDD
Your script is able to decide if every files of folderB are available in folderA
It doesn’t check that the items are identical and it doesn’t check if folderA contain items which aren’t in folderB.

So I’m wondering if your test is precise enough for the not precisely defined requirements.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) jeudi 5 mai 2016 10:50:48

Thanks guys, I should probably elaborate on what I’m trying to do.
I have a hot folder where people will drop a folder containing x number of PDF files, these will then be processed and the results will end up in a Distiller Out folder - basically once the total number of files in the Out folder match the number of files in the original folder I’d like the script to then delete the original folder.

Is that clearer, I wrote my original post quite late so apologies if it wasn’t that clear.

Yvan - I completely agree, a match on name only is risky as the script is not checking to see if the file contents are actually the same.

BigDrunk - Based on your clarification you could just count and compare the number of items in each folder and carry out actions until they are equal. Quite a few ways to do this - here are 2


--Set folder paths
set folderA to "HDD:Users:username:Desktop:FolderA:" as alias
set folderB to "HDD:Users:username:Desktop:FolderB:" as alias


--Create a list of folder contents
tell application "System Events"
	set listA to get the name of every disk item of folderA
	set listB to get the name of every disk item of folderB
end tell


--If Statement
if (count of listA) is equal to (count of listB) then
	--delete folder
else
	--do stuff
end if


--OR use a repeat
repeat until (count of listA) is equal to (count of listB)
	--do stuff
end repeat

As English is not my native language I have a doubt about : “I have a hot folder where people will drop a folder containing x number of PDF files

Must I understand that people drop the PDF files embedded in a source folder into the hot folder or are there supposed to drop the folder itself - of course with the embedded PDFs ?

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) jeudi 5 mai 2016 12:49:26

The users will drop a folder containing PDFs into the hot folder - the name of that folder will be different every time depending on what the PDFs are for. Does that help?

As far as I know a “hot folder” is a folder to which is attached a folder action script.
Here is the script to attach to the used one :

on adding folder items to this_folder after receiving added_items
	
	set addedFolder to item 1 of added_items
	set destFolder to (path to desktop as text) & "OutFolder:"
	
	--Create lists of folder contents for each folder
	tell application "System Events"
		repeat
			set listAdded to every disk item of addedFolder whose type identifier is "com.adobe.pdf"
			set listDest to every disk item of folder destFolder whose type identifier is "com.adobe.pdf"
			if (count listA) = (count listB) then exit repeat
		end repeat
	end tell
	
	tell application "Finder"
		move hotFolder to trash # Safer during tests. We may retrieve the folder in the trash
	end tell
	--	tell application "System Events" to delete hotFolder # More efficient but the folder would be really deleted
end adding folder items to

It’s a draft script so it doesn’t check that the first added item is really a folder.
Make tests with the posted code.
If it fits your needs it would be time to remove the three instructions triggering the Finder and activate the one asking System Events to delete the added folder.

Yvan KOENIG running El Capitan 10.11.4 in French (VALLAURIS, France) jeudi 5 mai 2016 17:04:48