Selecting Every nth File from Folder and Duplicating them to New Folder (continued)
This a follow-up question to VikingOSX's answer from this post: Selecting Every nth File from Folder and … - Apple Community.
I attempted to reproduce the solution, however, I get this error when I run it.
This is the script:
!/bin/zsh
#
: <<'COMMENT'
Select nth files from the SRCDIR and copy them into the DSTDIR.
COMMENT
typeset -gi mod=0
typeset -ga ary=()
function usage () {
printf '%s %s\n' "${ZSH_ARGZERO}" sourcefolder destfolder nth_value"
printf '%s %s\n' "${ZSH_ARGZERO}" "~/Desktop/TestZ ~/Desktop/BAR 3"
return
}
# three arguments or bust
((${#} == 3)) || ( usage;exit 1 )
# the nth value
mod=$3
# force relative path references to absolute paths
SRCDIR="${1:a:s/\~\//}"
DSTDIR="${2:a:s/\~\//}"
# do these folders exist?
[[ -d "${SRCDIR}" ]] || ( usage;exit 1 )
[[ -d "${DSTDIR}" ]] || ( mkdir "${DSTDIR}" )
# put every nth file into the array
ary=( $(printf '%s\n' "${SRCDIR}"/*.*(.Non) | awk -v m="${mod}" 'NR>1 && NR%m == 1') )
# sort array items in (o) ascending name order
for f in ${(o)ary[@]};
do
# if file exists in destination folder, don't process it
[[ -s "${DSTDIR}/${f:a:t}" ]] && continue
# copy the file from the source folder to the destination folder
printf '%s -> %s\n' "${f}" "${DSTDIR}/${f:a:t}"
cp -p "${f}" "${DSTDIR}/${f:a:t}"
done
exit 0
Any help would be appreciated, thanks!