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!

Posted on Aug 1, 2022 5:02 AM

Reply
Question marked as Top-ranking reply

Posted on Aug 1, 2022 8:02 AM

I cannot explain how that 2020 post arrived with an error in it. Two simple fixes and the code works on Big Sur 11.6.8:


#!/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}"/*.txt(.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 -a "${f}" "${DSTDIR}/${f:a:t}"
done
exit 0


I have 40 text files in my ~/Desktop/WIP folder and I want to copy (with attributes) every 5th file to the ~/Desktop/Foo folder. Here is an example run in the Terminal application. You wouldn't see this output in Automator.


odin: ~ % nfolders.zsh ~/Desktop/WIP ~/Desktop/Foo 5
/Users/viking/Desktop/WIP/test_006.txt -> /Users/viking/Desktop/Foo/test_006.txt
/Users/viking/Desktop/WIP/test_011.txt -> /Users/viking/Desktop/Foo/test_011.txt
/Users/viking/Desktop/WIP/test_016.txt -> /Users/viking/Desktop/Foo/test_016.txt
/Users/viking/Desktop/WIP/test_021.txt -> /Users/viking/Desktop/Foo/test_021.txt
/Users/viking/Desktop/WIP/test_026.txt -> /Users/viking/Desktop/Foo/test_026.txt
/Users/viking/Desktop/WIP/test_031.txt -> /Users/viking/Desktop/Foo/test_031.txt
/Users/viking/Desktop/WIP/test_036.txt -> /Users/viking/Desktop/Foo/test_036.txt
odin: ~ %


Similar questions

2 replies
Question marked as Top-ranking reply

Aug 1, 2022 8:02 AM in response to sot_kat

I cannot explain how that 2020 post arrived with an error in it. Two simple fixes and the code works on Big Sur 11.6.8:


#!/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}"/*.txt(.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 -a "${f}" "${DSTDIR}/${f:a:t}"
done
exit 0


I have 40 text files in my ~/Desktop/WIP folder and I want to copy (with attributes) every 5th file to the ~/Desktop/Foo folder. Here is an example run in the Terminal application. You wouldn't see this output in Automator.


odin: ~ % nfolders.zsh ~/Desktop/WIP ~/Desktop/Foo 5
/Users/viking/Desktop/WIP/test_006.txt -> /Users/viking/Desktop/Foo/test_006.txt
/Users/viking/Desktop/WIP/test_011.txt -> /Users/viking/Desktop/Foo/test_011.txt
/Users/viking/Desktop/WIP/test_016.txt -> /Users/viking/Desktop/Foo/test_016.txt
/Users/viking/Desktop/WIP/test_021.txt -> /Users/viking/Desktop/Foo/test_021.txt
/Users/viking/Desktop/WIP/test_026.txt -> /Users/viking/Desktop/Foo/test_026.txt
/Users/viking/Desktop/WIP/test_031.txt -> /Users/viking/Desktop/Foo/test_031.txt
/Users/viking/Desktop/WIP/test_036.txt -> /Users/viking/Desktop/Foo/test_036.txt
odin: ~ %


This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Selecting Every nth File from Folder and Duplicating them to New Folder (continued)

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.