Page 1 of 1

Script for sorting files into folders

PostPosted: Wed May 01, 2013 8:59 am
by kimaldis
I use this on my downloads folder for sorting files into folders based on strings in the filename. You need a folder tree, for example:

Code: Select all
Main
    Dave
    Pete
    Harry

and in the above, any incoming files with 'Dave' anywhere in their names will be moved to the 'Dave' folder, any with 'Pete' in the Pete folder .... etc. Matches are case insensitive.

Use:
Create a new rule and add a "Passes Shell script" condition, Embedded script. Edit the script, copy the script below into it, change the RootFolder variable to the full path of your folder tree and set the shell to /bin/bash. Bish, Bosh.

Comment out the growlnotify line if you don't have growl installed.

Enjoy

Code: Select all
#script:
RootFolder=/Users/Kim/Downloads/Movies

# split only on new line
OLDIFS="$IFS"  # save it
IFS="
"
file="$1"
for folder in $(ls -1 $RootFolder/)  ; do

   ufolder=`echo $folder | tr "[:upper:]" "[:lower:]"`
   ufile=`echo $file | tr "[:upper:]" "[:lower:]"`
   
   if grep -q $ufolder <<< $ufile ; then
      mv $file to $RootFolder/$folder
      /usr/local/bin/growlnotify -m "Moved $file to $folder in Movies" MovieMover
      exit 0
   fi

done

IFS=OLDIFS

exit 1

Re: Script for sorting files into folders

PostPosted: Wed May 01, 2013 9:00 am
by kimaldis
Sorry ... and set the action to 'ignore'

Re: Script for sorting files into folders

PostPosted: Tue Jun 04, 2013 3:44 pm
by emikysa
Thank you for sharing this! I found it incredibly powerful.

Re: Script for sorting files into folders

PostPosted: Wed Jun 08, 2016 10:54 am
by freshlesh3
Does this script still work? Can you expand a little more on the folder tree. I haven't been able to get this to work.

Re: Script for sorting files into folders

PostPosted: Thu Jun 30, 2016 8:57 am
by kevnm67
Kim (or other posters with issues), you can use the following for the root folder:

#script:
RootFolder=~/Downloads/Movies
- or any folder you want to use this script for.

Rather than the absolute path (making it useful for all user accounts).

Re: Script for sorting files into folders

PostPosted: Sun Jul 10, 2016 12:45 pm
by Forlando952
kimaldis wrote:Sorry ... and set the action to 'ignore'


Where we insert this command in the string?