Want to highlight a helpful answer? Upvote!

Did someone help you, or did an answer or User Tip resolve your issue? Upvote by selecting the upvote arrow. Your feedback helps others! Learn more about when to upvote >

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

Sorting of files into folders based on parts of a file name

Hello,


I have an image library I need to build for work. The images have been saved as this as an example


Dolce & Gabbana - The One_EDT_50ml.jpg


There are 1000s of brands, so I need a script that looks for the portion of the file name before the hyphen. and creates a folder based on all the images that fall under that brand.


Can anyone assist?


I've never used scripts before but any solution I can work with will work for me.


thanks,

iMac, OS X El Capitan (10.11.6)

Posted on Aug 3, 2016 7:13 AM

Reply
Question marked as Best reply

Posted on Aug 3, 2016 4:05 PM

Use the following:


tell application "Finder"

repeat until (count files of window 1) is 0

set the_offset to offset of " - " in (get name of file 1 of window 1)

set the_name to items 1 thru (the_offset - 1) of (get name of file 1 of window 1) as string

move (every file of window 1 whose name starts with the_name) to (make new folder at window 1 with properties {name:the_name})

end repeat

end tell


(143816)

17 replies

Aug 4, 2016 11:18 AM in response to Johnny Clulow

Hello


The original scripts will overwrite (replace) duplicate file in destination.


The script revised as below will not move the file which has duplicate name in destination so that the duplicate in destination is preserved and such source file will remain in the source directory.



set d to (choose folder with prompt "Choose image folder")'s POSIX path do shell script "/bin/bash -s <<'EOF' - " & d's quoted form & " exec 2>&1 SRC=$1 cd \"$SRC\" || exit shopt -s nullglob for f in *.jpg do [[ $f =~ ^([^-]+)\\ - ]] || continue d=${BASH_REMATCH[1]} [[ -d $d ]] || mkdir -p \"$d\" || continue mv -n \"$f\" \"$d\" done EOF"



It is also possible to move duplicate file with new name such as A-1.jpg, A-2.jpg for A.jpg if A.jpg is already present in destination. But we need to know details of re-naming convention to implement it in script.


After all, it depends upon what you want to do with the duplicate names in source and destination. Keep all or only the newest or else?


Regards,

H

Sorting of files into folders based on parts of a file name

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