Q: 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
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
Posted on Aug 4, 2016 6:38 AM