Support Forums

Full Version: File Organizer
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This script I made will find all of the files of a certain extension and organize them into one folder. Just give it a folder to start with, a destination folder(for everything it finds), the extension you want (.mp3, .exe, .txt, .py, etc), then give it the depth of sub folders to look in. Giving it the depth basically tells it how many folders deep it need to search, i.e:
If you want it to go this deep:
/home/devin/Documents/Coding/Python/My_Projects/Under_Development/GUI
You would need to give it a depth of 8, it acts like a spider kind of, it will search all possible directories within the given depth. If you are not sure just give it a depth like 20.
Code:
###############################
#This script will seach for files
#of the extension given by the
#user and will organize all of
#the files into a folder
# Made by : uber1337
###############################
import shutil, glob

def Search():
        start = str(raw_input('Give me a folder to start with:'))
        ext = str(raw_input('Give me the extenstion to search for(ie: .exe):'))
        end = str(raw_input('Where shall I put all the files?:'))
        depth = int(raw_input('How deep shall I look in terms of subdirectories?:'))
        symbol = '/*'
        for x in range(depth):
            for x in glob.glob('%s%s%s' %(start, (symbol * x), ext)):
                shutil.move(x, end)
                print 'I have moved %s to => %s\n' %(x, end)

if __name__ == '__main__':
    Search()
Enjoy Blackhat

EDIT:

Well this tool has been working out great for me so I took the time to make a GUI and a terminal interface.

Here is the GUI version:

Code:
import glob, shutil
from Tkinter import *

class MyGui(Frame):
    def __init__(self, parent=None):
        global start, ext, end, depth, status
        top = Tk()
        top.title('Seach Bot')
        Frame.__init__(self, parent)
        Label(text='Start').pack()
        start = Entry()
        start.pack()
        Label(text='Extension').pack()
        ext = Entry()
        ext.pack()
        Label(text='Destination').pack()
        end = Entry()
        end.pack()
        Label(text='Depth').pack()
        depth = Entry()
        depth.pack()
        Label(text='Status').pack()
        status = Entry()
        status.pack()
        button1 = Button(self, text='Go!', command=self.Search).pack(side=BOTTOM)
        button2 = Button(self, text='Clear', command=self.Clear).pack()
    def Search(self, parent=None):
        global start, ext, end, depth, status
        startx = start.get()
        extx = ext.get()
        endx = end.get()
        depthx = depth.get()
        depthx = int(depthx)
        symbol = '/*'
        for x in range(depthx):
            for x in glob.glob('%s%s%s' %(startx, (symbol * x), extx)):
                shutil.move(x, endx)
                status.delete(0, END)
                status.insert(0, 'Working...')
            status.delete(0, END)
            status.insert(0, 'Done!')    
    def Clear(self, parent=None):
        global start, ext, end, depth, status
        start.delete(0, END)
        ext.delete(0, END)
        end.delete(0, END)
        depth.delete(0, END)
        status.delete(0, END)
        
if __name__ == '__main__':
    window = MyGui()
    window.pack()
    window.mainloop()

And here is the terminal interface version, it works like this:
python filename start destination extension depth
here is an example, I have the file name as search.py:
python search.py /home/devin /home/devin/Documents .ogv 2
In the example, "search.py" is the filename, "/home/devin" is the starting directory, "/home/devin/Documents" is the destination, ".ogv" is the extension, and "2" is depth.

Here is the code:

Code:
import shutil, glob, sys

def Search():
        start = sys.argv[1]
    end = sys.argv[2]
        ext = sys.argv[3]        
        depth = sys.argv[4]
        symbol = '/*'
        depth = int(depth)
        for x in range(depth):
            for x in glob.glob('%s%s%s' %(start, (symbol * x), ext)):
                shutil.move(x, end)
                print 'I have moved %s to => %s\n' %(x, end)

if __name__ == '__main__':
    Search()

If you have any questions feel free to ask.
Edit: the indentation in the terminal one is fudged up, go ahead and tab "end"
thx for the code man
Glob comes in handy, however os.walk is definetly worth looking into, if you have not already.
Haha, this could be interesting to play around with. Big Grin
Thanks.

Btw, how did you learn Python? PM me please.
Hmm I wish I knew Python. I have no clue what this says.
helpful code dude! thanks for share!