Archive and Clean up of Dockets - omitting folders

I’m looking for some help in archiving docket jobs.
Once a docket job is complete it contains folders/files that need to deleted, while others are preserved when moving to a archived folder location. I’ve had some great assistance from James Nierodzik (an awesome contributor on these forums) in the past with another challenge. I’ve tried to modify an existing script we worked on, but I can’t get it working.

The scenario:

  • multiple users wanting to archive their docket jobs to a location on a file server
  • choose dockets jobs via a dialog and drag ‘n’ drop support
  • scanning the contents of the docket job folder and preserving select folders (a defined list)
  • removing the rest of the folders/files
  • re-locating the docket job folder to the final resting place on the file

A standard folder structure would look like this. Anything marked with an asterisk we want to keep. Some subfolders would be deleted, while other would not. Would I need to identify each folder name that I want to keep? I used numbers to keep it simple, where the folders’ names of the ones to be deleted would vary while the folders’ names that would be kept would be consistent for each job. The structure involves many more folders and subfolders, but I figure this would cover my bases, and make adjustments as necessary.

  • 01 **
  • 02 **
    – 0201 **
    – 0202 (delete)
  • 03 (delete)


property cleanFolder : missing value
property jobFolder : missing value

on open theJobs
	
	repeat with aJob in theJobs
		my cleanUp(aJob)
	end repeat
	tell application "Finder"
		beep 1
		display alert "Clean up and archive complete!" message "You will find all archived dockets in " & return & cleanFolder & return & return & "Please ensure they are backed up." buttons {"OK"} default button 1 giving up after 20
	end tell
end open

on run
	
	tell application "Finder"
		try
			jobFolder as alias
		on error
			display alert "Where is your Docket Archive folder?" message "We can't find the folder because:" & return & return & tab & "a) this is your first time archiving a docket" & return & tab & "b) the Docket Archive folder has been moved" & return & tab & "c) the Docket Archive folder has been renamed" & return & tab & "d) the Docket Archive folder has been deleted" & return & return & "Please select the Docket Archive folder" buttons {"Cancel", "Select Folder."} cancel button 1 as warning
			set jobFolder to (choose folder with prompt "Please select the Docket Archive folder") as Unicode text
		end try
		
		set theJobs to (choose folder with prompt "Please select the dockets to clean up" default location (jobFolder as alias) with multiple selections allowed)
	end tell
	
	repeat with aJob in theJobs
		my cleanUp(aJob)
	end repeat
	tell application "Finder"
		beep 1
		display alert "Clean up and archive complete!" message "You will find all archived dockets in " & return & cleanFolder & return & return & "Please ensure they are backed up." buttons {"OK"} default button 1 giving up after 20
	end tell
end run

on cleanUp(theJob)
	tell application "Finder"
		try
			cleanFolder as alias
		on error
			display alert "Where is your Docket Archive folder?" message "We can't find the folder because:" & return & return & tab & "a) this is your first time archiving a docket" & return & tab & "b) the Docket Archive folder has been moved" & return & tab & "c) the Docket Archive folder has been renamed" & return & tab & "d) the Docket Archive folder has been deleted" & return & return & "Please select the Docket Archive folder" buttons {"Cancel", "Select Folder."} cancel button 1 as warning
			set jobFolder to (choose folder with prompt "Please select the Docket Archive folder") as Unicode text
		end try
		set choice to ""
		try
			set jobName to name of theJob
			(cleanFolder & jobName) as alias
			set choice to button returned of (display alert "The docket \"" & jobName & "\" already exits in" & return & "the Docket Archive folder" message "What action would you like to take?" buttons {"Replace", "Skip"})
		end try
		if choice is in {"", "Replace"} then
			try
				set theFiles to every file of entire contents of theJob as alias list
			on error
				set theFiles to every file of entire contents of theJob as alias as list
			end try
			repeat with aFile in theFiles
				
				-- isolated the clean up routine to ONLY include the Products folder and NOT the other root folders in the job
				
				if modification date of aFile is equal to creation date of aFile and POSIX path of aFile contains "Products" then
					do shell script "rm " & quoted form of (POSIX path of aFile)
				end if
				(*
				if modification date of aFile is equal to creation date of aFile then
					do shell script "rm " & quoted form of (POSIX path of aFile)
				end if
*)
			end repeat
			
			try
				set theFolders to every folder of entire contents of theJob as alias list
			on error
				set theFolders to every folder of entire contents of theJob as alias as list
			end try
			repeat with aFolder in theFolders
				try
					set theFiles to every file of entire contents of aFolder
				on error
					try
						do shell script "rm -r " & quoted form of (POSIX path of aFolder)
					end try
				end try
			end repeat
			
			move theJob to (cleanFolder as alias) with replacing
		end if
	end tell
end cleanUp


The easiest way to discern the folders and files you want to keep is to put their names in a list (either a static list or a dynamic one you create by adding dates or other identifying info to the names). Then you can test for membership in the list by

if (name of the folder) is in folderList then....

Does this help?