Selecting Every nth File from Folder and Duplicating them to New Folder

So for example, if I have folder with 99 Files, and I want to only select every 3rd File, beginning with that 3rd File, and duplicate only these Files to a New Folder, how would I accomplish this using either Apple Script or Automator?


(The 2 responses on a previous question from 2012 by friedmak from red_menace and Frank Caggiano do NOT work properly.)


This should not be that difficult, I am surprised that there is no answer for this as far as I have searched.


Goal would be able to Choose Source Folder, Choose Destination Folder (or have it automatically created), choose beginning Indexed Starting File, Choose Interval (ie every 2nd, 3rd, 4th, 5th etc), and either Move or Duplicate these Files to the Destination Folder


Thank You!


iMac 21.5″, macOS 10.14

Posted on Oct 21, 2020 10:46 AM

Reply
Question marked as Top-ranking reply

Posted on Oct 22, 2020 2:18 PM

And here is the AppleScript solution that duplicates the results of the previously submitted Zsh script for the given nth file selection. The AppleScript will run slower than a Zsh solution on larger file collections. I have provided a link in the script back to the 2013 discussion, where this current AppleScript solution was derived, so credit to those who it is due.


-- nth.applescript
(*
	Select nth file of source folder and copy to destination folder
	Derived from reference: https://discussions.apple.com/thread/5553356
	Tested: macOS 10.14.6 (18G6032), macOS 10.15.7 (19H2)
	VikingOSX, 2020-10-22, Apple Support Communities, no warranties expressed/implied
*)

set m to (text returned of (display dialog "Enter your nth file skip value: " default answer "3")) as integer
set srcFold to (choose folder with prompt "Select your source folder:" default location (path to desktop))
set destFold to (choose folder with prompt "Select/Create your destination folder:" default location (path to desktop))

tell application "Finder"
	set nthList to the sort (files of srcFold) by name
	if not (length of nthList) > 0 then return
	
	repeat with n from (m + 1) to (count of nthList) by m
		-- log (item n of nthList) as text
		duplicate (item n of nthList) to folder destFold
	end repeat
end tell
return



As an example, I have a folder with fifty files in sequential naming order from file01.txt ... file50.txt. This script with an nth selection of 3 will result in selected files: file04.txt, file07.txt, file10.txt, ... file49.txt.

Similar questions

5 replies
Question marked as Top-ranking reply

Oct 22, 2020 2:18 PM in response to VikingOSX

And here is the AppleScript solution that duplicates the results of the previously submitted Zsh script for the given nth file selection. The AppleScript will run slower than a Zsh solution on larger file collections. I have provided a link in the script back to the 2013 discussion, where this current AppleScript solution was derived, so credit to those who it is due.


-- nth.applescript
(*
	Select nth file of source folder and copy to destination folder
	Derived from reference: https://discussions.apple.com/thread/5553356
	Tested: macOS 10.14.6 (18G6032), macOS 10.15.7 (19H2)
	VikingOSX, 2020-10-22, Apple Support Communities, no warranties expressed/implied
*)

set m to (text returned of (display dialog "Enter your nth file skip value: " default answer "3")) as integer
set srcFold to (choose folder with prompt "Select your source folder:" default location (path to desktop))
set destFold to (choose folder with prompt "Select/Create your destination folder:" default location (path to desktop))

tell application "Finder"
	set nthList to the sort (files of srcFold) by name
	if not (length of nthList) > 0 then return
	
	repeat with n from (m + 1) to (count of nthList) by m
		-- log (item n of nthList) as text
		duplicate (item n of nthList) to folder destFold
	end repeat
end tell
return



As an example, I have a folder with fifty files in sequential naming order from file01.txt ... file50.txt. This script with an nth selection of 3 will result in selected files: file04.txt, file07.txt, file10.txt, ... file49.txt.

Oct 21, 2020 9:01 PM in response to sneakyworm

The ability to select nth files for processing on macOS requires a programming solution, as modulo file selection is not built into the Finder, and has to be poked at in UNIX shells to achieve it.


Here is a solution that does the following things from a double-clicked, Automator: Application on the Desktop:

  1. Prompts for the Nth file integer
  2. Prompts for the source directory
  3. Prompts for the destination directory, and allows you to create, and select one.
  4. Uses strengths of Zsh shell
  5. Creates a working array of nth filenames selected from the source directory
  6. Sorts the working array by filename, copying files in order to the destination directory
    1. If a file already exists in the destination directory, it will not be copied
  7. Workflow ompletion message


Here is the Automator workflow that I have tested on Mojave 10.14.6 (18G6032), but due to the late hour, not on Catalina 10.15.7 (19H2). Right-click on this image and choose View from the secondary menu so you can zoom into it for legibility:



When you add the Automator Utility Library : Run Shell Script action, choose these settings, and remove any defauilt for-loop in the action's body, as it will be replaced with the following Zsh shell script code:


You will copy and paste the entire contents of the following script into the now blank Run Shell Script action:


!/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



Oct 21, 2020 1:21 PM in response to Niel

Good effort, close, thank you, maybe we can get this situated.


I honestly don't understand what the deal is.


So i actually changed the starting "repeat with this_file from 1" to "3" and that was a slight improvement, however, like every other script I've tried, where the real errors occur is at every "10" (ie 20, 30, 40 etc)


ie.

3,6,9,12,15,18,20,23,26,29,31,34,37,42,45,48,50,53,56,59,61,64,67,72, etc


Something weird is happening there and it's been flabberghasting, but hey maybe you can solve this Niel!


I seriously don't understand why "Custom Selecting" such as "Every Nth" is not just a Standard Option from Apple...


So much mediocrity everywhere it's odd...


We can do this!

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

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