Script for sorting files into folders

From your noodle to other noodles. Talk about ways to get the most from Hazel. Even exchange recipes for the cool rules you've thought up. DO NOT POST YOUR QUESTIONS HERE.

Moderators: Mr_Noodle, Moderators

Script for sorting files into folders 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
kimaldis
 
Posts: 2
Joined: Wed May 01, 2013 8:48 am

Re: Script for sorting files into folders Wed May 01, 2013 9:00 am • by kimaldis
Sorry ... and set the action to 'ignore'
kimaldis
 
Posts: 2
Joined: Wed May 01, 2013 8:48 am

Re: Script for sorting files into folders Tue Jun 04, 2013 3:44 pm • by emikysa
Thank you for sharing this! I found it incredibly powerful.
emikysa
 
Posts: 2
Joined: Wed Mar 27, 2013 2:36 pm

Re: Script for sorting files into folders 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.
freshlesh3
 
Posts: 4
Joined: Sun Oct 25, 2015 10:10 am

Re: Script for sorting files into folders 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).
kevnm67
 
Posts: 2
Joined: Sat Nov 29, 2014 11:33 pm

Re: Script for sorting files into folders 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?
Forlando952
 
Posts: 5
Joined: Thu Jul 07, 2016 6:04 pm


Return to Tips & Tricks - DO NOT POST QUESTIONS