Matching Folder Names to Partial File Names in Different Directories

Hey all

Really having a hard time with this one. Not even sure how to start.

When a job is created, it creates two things: 1) a File and 2) a Folder with the name of the File. But the File is created in my “Jobs” directory and the Folder is created in my “Temp” directory. For example, Temp directory may have the Folder:

Johns.Pizza.Place
Marks.Hair.Salon.NY
Rachels.Bike.and.Boat.Shop

and the Jobs directory will have Files called:

Johns.Pizza.Place.5489231.jpg
Marks.Hair.Salon.NY.888.jpg
Rachels.Bike.and.Boat.Shop.11122287865.jpg

As you can see, the Folder names always match part of the Filenames but each File is always appended with a random number of length and then the extension (which is always a jpg).

What I want to do is be able to run a script that tries to 1) match the the Folder names in the directory of Files BUT only is doing a partial match since the Files have both a random number and extension added, then 2) Deletes those folders when it DOES NOT FIND an associated File (I am using Hazel to delete the Files after certain actions). While the Files are present I dont want the Folders deleted.

I just cant seem to figure out how tell applescript to take the Folder name and then tell it to only match those characters in the Filename

Any help is greatly appreciated.

Hello.

First of all you have to make a handler that strips off the two last parts of your filename then you can try this handler.
(Try playing with it, and see if it does what it should, it is not tested.) It should really return the folder as a posix path.)

I am not sure if I understand your problem anyway, but still thinks the handler will come to use.

It works so that you can specify an asterisk for a partial match. So that when you are having your folder name, then you can search in that directory and see if you find a matching file name.

You may want to google mdfind, mdls on a jpeg file should show you some of those attributes that may identify a jpg file, should you need to be absolutely sure.


set inputFolder to quoted form of POSIX path of (choose folder)

set searchName to "\"" & "Johns.Place.NY" & "*\"" # You'll enter your foldername here, but keep the surroundings of the variable.

set res to findFilesOnly(searchName, inputFolder)
if res = {} then 
# Delete the folder
end if
on findFiles(searchterm, directoryToSearchIn)
	try
		return every paragraph of (do shell script "mdfind -onlyin " & directoryToSearchIn & " 'kMDItemFSName  = " & searchterm & " && kMDItemContentType != \"public.folder\"' ")
	on error
		return {}
	end try
end findFiles

tell application "Finder"
	set filelist to name of every file of currdest
	set deleteit to true
	repeat with f in filelist
		if f contains (name of currdest) then
			set deleteit to false
			exit repeat
		end if
	end repeat
	if deleteit then
		delete currdest
	end if
end tell


assuming currDest is a reference to a Finder folder

Thank you for the quick reply!

I tried running the script just to see results. I get the following error: “error “«script» doesn’t understand the findFilesOnly message.” number -1708 from «script»”. Not sure why.

Also, I want to be able to set the script to run say every hour which I can do in cron. But is there a way to run the script such that it does not require choosing the folder and inputting actual folder name? So just compare every folder in the one directory to the filenames in 2nd directory and delete any not found. And it just reruns every hour.

Thank you again.

Yes. Sorry. I mistyped. I want to take folder name and compare to a filename, both in different directories. Let me see if I can adapt this. Thanks!

The reason for that, I surmise is that you call the handler from within a tell application “Finder” block. Then you would have to put my in front of the handler in order for the script to “see” it from within the tell block.