You are currently viewing macOS 批量壓縮資料夾

Batch compress multiple folders into individual zip files on macOS

Preface

Suddenly one day, when I was uploading resources to CxDrive, I needed to package each subfolder into a zip, and it was really too tiring to do it manually one by one, so why not write a script for this kind of thing?

Tutorial

  1. First, we search for Automator through Spotlight and open it.
CleanShot 2022-02-07 at 11.15.38@2x
  1. Add a new Quick Action
CleanShot 2022-02-07 at 11.02.35@2x
  1. Search Run Shell
CleanShot 2022-02-07 at 11.02.48@2x
  1. Select the corresponding configuration, workflow purpose: files or folders , location:Finder
CleanShot 2022-02-07 at 11.02.25@2x
  1. Drag the Run Shell Script to the right and type in the command 👇🏻
for f in "$@"; do

    dn="$(dirname "$f")"
    bn="$(basename "$f")"

    cd "$dn" || exit

    if [[ ! -e "$bn.zip" ]]; then
        ditto -c -k --sequesterRsrc --keepParent "$f" "$bn.zip"
    else
        n=2
        for i in $bn *.zip; do
            if [[ "$bn $n.zip" == "$i" ]]; then
                n="$(( n + 1 ))"
            fi
        done
        ditto -c -k --sequesterRsrc --keepParent "$f" "$bn $n.zip"
    fi

done

afplay /System/Library/Sounds/Purr.aiff

Demo

Conclusion

Don't do repetitive things, we only need to be responsible for innovation.

References

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.