Create ZIP files from containing related files from multiple folders

archive-utilityautomatorzip

Is there a way to automate creation of ZIPs containing files with common name elements?

What I have are multiple variations of the same image:

files/2250/8199print.jpg

files/1200×1200/8199square.jpg

files/1200×900/8199wide1.jpg

files/1200×628/8199wide2.jpg

files/480×360/8199small.jpg

What I'd like to do is create some sort of application that goes into folders, finds related files (all contain "8199") and then archives them all into a new file named "cartoon8199.zip".

I tried to create an Application via Automator, but couldn't figure out how to make the Name Contains field in Find Finder Items include a variable.

Any ideas on how to achieve this?

I'm on a Mac running HighSierra 10.13.3.

Thanks in advance!

Best Answer

Heres your script as promised. Any questions or suggestions? Comment below :)

What the script does: Searches through a selected folder for folders. Searches through each one of those for files. If those files contain a specified search parameter then the file is moved to a folder in a location specified. Once all files are checked, the files in the folders at the location specified is ziped up.

For what you want: First Prompt choose the /files folder. In the second prompt enter 8199.

The Script:

set myFolder to choose folder with prompt "Choose a Folder" # Choose the /files folder
display dialog "Enter Your Search Paramaters" default answer "" #Enter "8199"
set mySearch to the text returned of the result
set myZipLocation to the POSIX path of (choose folder with prompt "Choose where to save your ZIP file")
display dialog "Enter the Name of Your Compress File" default answer ""
set myZip to the text returned of the result
tell application "Finder"
    set myFolders to every folder in folder myFolder
    set myFiles to {""}
    repeat with i in myFolders
        set myFiles to myFiles & every file in i
    end repeat
    do shell script ("mkdir " & the quoted form of (myZipLocation & "/" & myZip))
    repeat with i from 2 to count of myFiles
        if the name of (item i of myFiles) contains mySearch then
            set myPOSIX to the quoted form of (the POSIX path of (item i of myFiles as alias))
            do shell script "cp " & myPOSIX & " " & the quoted form of ((myZipLocation & "/" & myZip) & "/" & the name of (item i of myFiles))
        end if
    end repeat
    do shell script "cd " & the quoted form of myZipLocation & "&& zip -r " & quoted form of (myZip & ".zip") & " " & quoted form of myZip
end tell