Apple Event: May 7th at 7 am PT

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
Question marked as Best reply

Aug 3, 2016 4:05 PM in response to Johnny Clulow

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)

Aug 3, 2016 1:24 PM in response to Johnny Clulow

Replace the first and last lines of the script with that text. In total:


tell application "Finder"

with timeout of 600 seconds

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 timeout

end tell


(143822)

Aug 3, 2016 3:59 PM in response to Niel

Niel, thank you for what you have wrote for me. It's perfect for running this on a handful of files but when we are talking 100s & thousands the whole process (Script & Finder) are taking literally hours to progress and I can't tell if it'll ever complete. It seems to produce one folder then nothing happens. Is there anything else we can do here? Or can it be run in a different way?

Aug 3, 2016 4:11 PM in response to Johnny Clulow

Try using:


tell application "Finder"

with timeout of 600 seconds

set the_files to files of window 1

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 the_files whose name starts with the_name) to (make new folder at window 1 with properties {name:the_name})

end repeat

end timeout

end tell


(143827)

Aug 4, 2016 6:38 AM in response to Johnny Clulow

Hello


You may try something like the following AppleScript script which is a mere wrapper of shell script. It will let you choose the source image folder and then move each *.jpg file thereof to folder named after prefix before the first " -" in file name.



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




Briefly tested under OS X 10.6.8 but no warranties. Please make sure you have complete backup of original folders before running this sort of script.


Good luck,

H

Aug 4, 2016 6:48 AM in response to Hiroto

Here's a minor revision to be a little safer in case of directory and file name conflict. Any error in shell script will be reported in result pane/window of Script Editor.



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 \"$f\" \"$d\" done EOF"



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.