Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Select only odd (or even) numbered files and move to new folder

Hello-

I am in need of assistance in creating a script to move files ending in numbers to a new folder if they are odd or even. The files are named like AH1.wav, AH2.wav ...AH12.wav ...AH111.wav, etc.

I have found a script that moves every other file in a selected folder to another selected folder:

set srcFldr to (choose folder with prompt "Select the source folder:")
set destFldr to (choose folder with prompt "Select the destination folder:")

tell application "Finder"
set fileList to files of srcFldr
set fileCnt to count fileList
if fileCnt > 1 then
repeat with i from 2 to fileCnt by 2
move item i of fileList to destFldr
end repeat
end if
end tell




The problem is that the files that end up in the destination folder are not only odd or even, so the "order" of the files in the fileList are not in the same order, by name, as they appear in alphanumeric order as described above. I know there is a way to check the character of the file name just before the "." and then use mod 2 on that value to conditionally select and move only even numbered files, but I'm not sure how to script that. Any help would be greatly appreciated!

Macbook, Mac OS X (10.5.8)

Posted on Feb 3, 2010 10:05 PM

Reply
2 replies

Feb 4, 2010 3:47 AM in response to mctconsortium

Hello

You may try the following script.
I think it will give the fileList sorted in the same order as in Finder.


set srcFldr to (choose folder with prompt "Select the source folder:")
set destFldr to (choose folder with prompt "Select the destination folder:")
tell application "Finder"
--set fileList to files of srcFldr -- sorted by name in HFS Plus' fashion (?)
set fileList to sort (get files of srcFldr) by name -- sorted by name in Finder's fashion
set fileCnt to count fileList
set ff to {}
repeat with i from 2 to fileCnt by 2
set end of ff to item i of fileList
end repeat
move ff to destFldr -- you can move list at once
end tell


*If I'm not mistaken, the original fileList not sorted by Finder will be in the order sorted by HFS Plus name using HFS Plus' FastUnicodeCompare algorithm when the volume is HFS Plus volume. And Finder's order is not equivalent to HFS Plus' order.

Hope this may help,
H

Select only odd (or even) numbered files and move to new folder

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