Can a zsh script to batch zip files work recursivly? (VikingOSX script in post)
Last month VikingOSX graciously coded a script to batch process particular sets of files into archive zips that take on the name of the files. From this post. It rocks. I've zipped about 50,000 files since then.
I have recently learned a new vocabulary word: recursive. Yes, I'm new to the command line.
Could the script below be changed so that it works recursively on all sets of designated files (jpg, png, eps, svg, in this case) in the directory AND subdirectories of that directory? If so, would it work even if some directories had no files? And would it ignore (not archive) any other file type than the ones designated in the script ((jpg, png, eps, svg)?
#!/bin/zsh
: <<"COMMENT"
For a given folder containing filenames identified by product code and suffix,
aggregate the four image files associated with it into a zip container.
For example: Dzn-4-n-m-no.zip contains the four images: Dzn-4-n-m-no.{eps,jpg,png,svg}
Tested: macOS 14.1.2 (Zsh 5.9), macOS 10.14.6 (Zsh 5.3)
Revision: 2
Author: VikingOSX, 2023-12-03, Apple Support Communities, No warranties implied.
Usage: lab.zsh folder
COMMENT
# absolute path of folder passed as only argument to the script
ZIPDIR="${1:a:s/\~\//}"
# image types to process
EXTPAT="eps|jpg|png|svg"
last_ext=
last_fname=
setopt nocaseglob
for f in "${ZIPDIR}"/*.(${~EXTPAT})(.Non);
do
# Two captures: product name and two or three character suffix
[[ "${f:t}" =~ "(^.*\-)([[:alpha:]]{2,3})\..*$" ]] || continue
[[ $last_fname != "${match[1]}" || $last_ext != "${match[2]}" ]] || continue
last_fname="${match[1]}"
last_ext="${match[2]}"
# capture unique suffixes to their respective zip file excluding dot files
zip -qj -6 "${ZIPDIR}/${(j::)match[@]}".zip "${ZIPDIR}/${(j::)match[@]}".{eps,jpg,png,svg} -x ".*"
# rm -f "${ZIPDIR}/${(j::)match[@]}".{eps,jpg,png,svg}
done
zipcnt=( "${ZIPDIR}"/*.zip )
echo "Zip archives created: $#zipcnt"
unset zipcnt
exit 0