move files into matching folders

Get help. Get answers. Let others lend you a hand.

Moderator: Mr_Noodle

move files into matching folders Sun Mar 14, 2010 12:44 am • by phrank303
I've searched in the whole forum, but unfortunately I wasn't able to find what I'm looking for, or there wasn't any solution yet. Please help.

I have serveral files and folders in my download folder.

I'm looking for an action, which looks If part of file name matches part of folder name , the files should be moved to its matching folder.

Like ABC-001.jpg will be moved into folder ABC and file DEF-001.jpg will be moved into folder DEF
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: move files into matching folders Tue Mar 16, 2010 11:43 am • by holigen
You probably can't do this with just Hazel. You might want to look at using either Folder Actions or Hazel's ability to run a script on a file when it matches.

If you're going to use a script, you should probably list the folders in the Downloads folder and then, whenever an image file is added, check to see if that image file name has a corresponding folder.

Here's some Python code that worked for me by using it as an "embedded" script for all images in the Downloads folder:

Code: Select all
import os, sys, re, shutil;

# get all the folders in the current folder and then check to see if the file being processed should be moved
for item in os.listdir(os.getcwd()):
    if os.path.isdir(item):
        expression = re.compile(os.path.basename(item), re.IGNORECASE);
   
        if expression.search(sys.argv[1]):
            shutil.move(os.path.abspath(sys.argv[1]), item);


Make sure that your "shell" is set to /usr/bin/python when adding the action. If you need more help with this, feel free to ask.
holigen
 
Posts: 18
Joined: Tue Mar 16, 2010 2:43 am

Re: move files into matching folders Tue Mar 16, 2010 1:30 pm • by phrank303
yeeeeaaah, it works. thanks a lot. :D

now as i've got you, one more thing / question.

is it maybe possible for the script to look inside subfolders and compare the names inside with the file outside,
if some similar name is outside the subfolder it will be moved inside to the similar filename?

p.s.are you fit in programming and automator?
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: move files into matching folders Tue Mar 16, 2010 3:50 pm • by holigen
phrank303 wrote:is it maybe possible for the script to look inside subfolders and compare the names inside with the file outside,
if some similar name is outside the subfolder it will be moved inside to the similar filename?


I think that I just answered this in your other topic in this forum.

phrank303 wrote:p.s.are you fit in programming and automator?


I'm a programmer by trade, so I would say that, yes, I am fit in programming. As for Automator... I'm not very comfortable making workflows since it just seems easier to use AppleScript. I will say that Automator has its uses: it seems to be easier than AppleScript to use for handling media like images and PDFs and for printing.

I haven't given it a serious look, though, since it debuted.
holigen
 
Posts: 18
Joined: Tue Mar 16, 2010 2:43 am

Re: move files into matching folders Tue Mar 16, 2010 5:06 pm • by phrank303
super, you are great. thanks a lot again. now i will go on for a test ride.
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: move files into matching folders Tue Apr 06, 2010 2:46 pm • by JustRed
Waaaay over my head! But I'm looking to do the same thing.

I'd like to copy downloaded app updates sitting in my downloads folder to their matching folders under my documents folder.

Hence, Hazel-2.3.5.dmg would copy itself from the downloads folder to a folder named Hazel.

Granted, If I had a lot of spare time, I suppose I could create a rule under downloads that would take any file containing Hazel and copy it to the Hazel folder. Then, I'd have to repeat this for every single app that I downloaded updates for. AND, I'd have about twenty or so rules! Also, if I had a photo that contained the word Hazel, it'd copy that, which I wouldn't want it to do. Just files with the word Hazel AND an .dmg extention.

I didn't see any way to make Hazel behave like smarfolders (hold the option key down to nest rules). I tried to get hazel to run a couple rules: 1) for any file containing .app, .dmg, .mkpg add to the comments: Move Me To The DMG Folder. Then, I created a second rule: 2) If Comment contained Move Me To The DMG Folder, then copy the file to Documents>DMGs. But alas, Hazel doesn't seem to like rules based off comments. The first rule which added the comment works fine. I tried both comments and spotlight comments, but no go.

Anyone know if this is on the horizon for future updates or versions?
JustRed
 
Posts: 20
Joined: Tue Apr 06, 2010 2:23 pm

Re: move files into matching folders Tue Apr 06, 2010 3:46 pm • by phrank303
unfortunately, i just have these scipts (above) from “holigen” which work okay, but not perfect.
i've checked “tidy up!” but it’s more for cleaning than sorting. so far no real solution yet…
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: move files into matching folders Tue Apr 06, 2010 3:52 pm • by JustRed
Well, hopefully, one day.
JustRed
 
Posts: 20
Joined: Tue Apr 06, 2010 2:23 pm

Re: move files into matching folders Wed Apr 07, 2010 3:58 pm • by Mr_Noodle
You don't have to create individual rules for all the cases. Search the help for "match patterns" and "custom tokens". If there's a pattern there, you can tell Hazel to detect it and pick apart the file name.

The only thing it can't do right now is the "if the folder already exists" part. What will happen is that the folder will be created if it doesn't exist.
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City

Re: move files into matching folders Wed Apr 07, 2010 4:14 pm • by JustRed
Great! Thanks! I'll search.

So, to clarify, if the folder already exists, that's fine? It'll dump the file into the 'matching' folder? Or, did you mean that Hazel will just create a new folder anyway even if one already exists?
JustRed
 
Posts: 20
Joined: Tue Apr 06, 2010 2:23 pm

Re: move files into matching folders Wed Apr 07, 2010 7:44 pm • by phrank303
@ Mr_Noodle,
Thanks for helping us, but after I’ve studied Hazel’s help file,
I’m still not able to do what I wanted to do. How can I move at “Do the following” matching files into an already existing folder with a matching name?

Please help, because this seems to be beyond my skills.
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: move files into matching folders Thu Apr 08, 2010 1:37 pm • by Mr_Noodle
Using the sort into subfolders action, Hazel will use an existing folder but if it doesn't exist, it will create it. What it can't do is only do the action if the folder already exists (i.e. not execute if the folder doesn't exist).
Mr_Noodle
Site Admin
 
Posts: 11865
Joined: Sun Sep 03, 2006 1:30 am
Location: New York City


Return to Support