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

Previous Page 2
  • by Johnny Clulow,

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

    Perfect. What would happen if the files already exist? Would there be an automatic overwrite, discard, corruption or duplication?

  • by Johnny Clulow,

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

    Is there any amendment to your code to allow for duplicated images files to be flagged or alter so no overwrite takes place?

    Thanks

  • by Hiroto,

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

Previous Page 2