Johnny Clulow

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

Close

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

  • All replies
  • Helpful answers

Page 1 Next
  • by Niel,Helpful

    Niel Niel Aug 3, 2016 4:05 PM in response to Johnny Clulow
    Level 10 (312,640 points)
    Mac OS X
    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)

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 12:16 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Aug 3, 2016 12:16 PM in response to Niel

    Hi,

     

    Really appreciate your answer but I don't know what to do with it. Like I said, I don't come from a programming background. I have the script editor open but not sure how to use it?

     

    Ignore the above in blue! Just run it and it works! Thats amazing!

     

    Thank you so much.

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 12:40 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Aug 3, 2016 12:40 PM in response to Niel

    This works on smaller collections but when I ask it to run against 100s it crashes finder?

     

     

    error "Finder got an error: AppleEvent timed out." number -1712

  • by Niel,Helpful

    Niel Niel Aug 3, 2016 4:05 PM in response to Johnny Clulow
    Level 10 (312,640 points)
    Mac OS X
    Aug 3, 2016 4:05 PM in response to Johnny Clulow

    Place the repeat loop inside:

     

    tell application "Finder"

    with timeout of 600 seconds

     

    end timeout

    end tell

     

    (143821)

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 1:22 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Aug 3, 2016 1:22 PM in response to Niel

    Sorry where does this go? I'm really REALLY new to this. But thank you for what you have given me so far. Works exactly as I wanted minus the timeout/hangs.

  • by Niel,

    Niel Niel Aug 3, 2016 1:24 PM in response to Johnny Clulow
    Level 10 (312,640 points)
    Mac OS X
    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)

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 1:56 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Aug 3, 2016 1:56 PM in response to Niel

    Thanks I'll give this a try.

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 3:59 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    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?

  • by Niel,

    Niel Niel Aug 3, 2016 4:11 PM in response to Johnny Clulow
    Level 10 (312,640 points)
    Mac OS X
    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)

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 3, 2016 11:34 PM in response to Niel
    Level 1 (4 points)
    Mac OS X
    Aug 3, 2016 11:34 PM in response to Niel

    I get this error immediately after running that script

     

    error "Can’t get {} whose name starts with \"Constance Carroll\"." number -1728

  • by Hiroto,Solvedanswer

    Hiroto Hiroto Aug 4, 2016 6:38 AM in response to Johnny Clulow
    Level 5 (7,286 points)
    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

  • by Hiroto,

    Hiroto Hiroto Aug 4, 2016 6:48 AM in response to Hiroto
    Level 5 (7,286 points)
    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

  • by Johnny Clulow,

    Johnny Clulow Johnny Clulow Aug 4, 2016 6:57 AM in response to Hiroto
    Level 1 (4 points)
    Mac OS X
    Aug 4, 2016 6:57 AM in response to Hiroto

    Hello Hiroto,

     

    That is absolutely perfect. Exactly what I needed. Sorted 2gig of files in 2 seconds. Do you know if that script would add files to existing folders if the name already exists?

  • by Hiroto,

    Hiroto Hiroto Aug 4, 2016 7:04 AM in response to Johnny Clulow
    Level 5 (7,286 points)
    Aug 4, 2016 7:04 AM in response to Johnny Clulow

    My pleasure! Glad to know it serves you well.

     

    And yes, the script will create destination folder only if it is not present. If destination already exists, script will use it.

     

    H

Page 1 Next