find maching file names in subfolder

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

Moderator: Mr_Noodle

find maching file names in subfolder Sun Mar 14, 2010 1:01 am • by phrank303
I have several files and folders in my download folder.

I'm looking for an action, which looks at matching content name of the several folders in the download folder,
if there are already files in the folder with matching names, the files in the download folder will be sorted into the same place of the other matching file name.

Downloads
ABC.txt
DEF.txt

folder-XY
-ABC.jpg (inside folder-XY)

folder-ZX
-DEF.jpg (inside folder-ZX)

ABC.txt should be placed into the folder-XY
and DEF should be placed into the folder-ZX


Hope somebody have a solution for me.
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm

Re: find maching file names in subfolder Tue Mar 16, 2010 3:46 pm • by holigen
This is a similar case to your other question, so the code that I posted there needed little modification to get it to work in this instance.

You set up the rule in a similar way to the other rule, except that you define the matching rules to find any text files (or whichever kind of file you want this rule to apply to). The embedded script to use for the "shell script" is again, in Python, so you'll need to change the shell to "/usr/bin/python."

Code: Select all
#!/usr/bin/env python

import os, sys, re, shutil;

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


Hope that helps. It seemed to work reasonably well for me when I tested it out. Note that if any file in the subfolder matches the file being matched, it will move that file into the subfolder immediately. Not sure if that's what you wanted, but it seems like the behavior you were looking for.
holigen
 
Posts: 18
Joined: Tue Mar 16, 2010 2:43 am

Re: find maching file names in subfolder Tue Mar 16, 2010 5:51 pm • by phrank303
thank you thank you thank you :P

perfect. makes my workflow a lot easier.
phrank303
 
Posts: 8
Joined: Sat Mar 13, 2010 11:37 pm


Return to Support

cron