LorenKPetrov

Download Sorter

Jan 13th, 2014
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.74 KB | None | 0 0
  1. # Module Imports #
  2. import os # imports the OS module
  3. # Directory Variables #
  4. start_dir = "RELEVANT DIR HERE" # The directory in which I save my files to.
  5. final_dir = ["Pictures\\","eBooks\\","WordDocs\\","PowerPoints\\","Applications\\","Torrents\\","Archives\\","ISO\\"] # List of the folders I have created for the relevant file types
  6. # File Variable list #
  7. files_typ = [".jpg",".jpeg",".gif",".png",".bmp",".pdf",".ePub",".rtf",".doc",".docx",".txt",".ppt",".pptx",".exe",".msi",".torrent",".zip",".rar",".tar.gz",".iso"] # List of common file types I have downloaded.
  8. # Directory Code Code #
  9. os.chdir(start_dir) # Changes to the relevant downloads directory that way it does not have to be executed from the same directory.
  10. dir_list = os.listdir("./") # Makes a list of all the current files in the directory.
  11. new_list = [] # New list for the list clean function to go to.
  12. # List clean Function #
  13. def list_clean(): # Because my downloads folder contains subdirs this code will clean them out of the list
  14.    
  15.     Y = 0 # Y variable
  16.  
  17.     for x in dir_list: # Counts the amount of files (folders and files) in the directory
  18.        
  19.         if os.path.isdir(dir_list[Y]) == True: # if it's a directory, removes it from the list
  20.             dir_list.remove(dir_list[Y])
  21.             list_clean() # repeats its self until there are no more directories
  22.         elif os.path.isfile(dir_list[Y]) == True: # if it's a file, adds it to a new list
  23.             new_list.append(dir_list[Y]) # adds the file to a new list
  24.             dir_list.remove(dir_list[Y]) # removes it from the old list completely
  25.             list_clean() # loops until there are no more files in the list
  26.    
  27. def file_move(): # Now it will move the files into the relative sub directories
  28.    
  29.     new_list.remove("desktop.ini") # desktop.ini is always included in folders, so removing it here is good practice.
  30.    
  31.     W = len(new_list) # Checks how many files are in the list
  32.     X = 0
  33.     Y = 0
  34.     Z = 0
  35.     while W > X: # A nested loop with a nested if statement.
  36.         if len(new_list) >= 1: # if statement
  37.             while Z < len(files_typ): # while statement
  38.                     if new_list[0].endswith(files_typ[Y]) == True:
  39.                         if new_list[0].endswith(files_typ[0]) == True:
  40.                             os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
  41.                             new_list.remove(new_list[0])
  42.                         elif new_list[0].endswith(files_typ[1]) == True:
  43.                             os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
  44.                             new_list.remove(new_list[0])
  45.                         elif new_list[0].endswith(files_typ[2]) == True:
  46.                             os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
  47.                             new_list.remove(new_list[0])
  48.                         elif new_list[0].endswith(files_typ[3]) == True:
  49.                             os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
  50.                             new_list.remove(new_list[0])
  51.                         elif new_list[0].endswith(files_typ[4]) == True:
  52.                             os.rename(start_dir+new_list[0],start_dir+final_dir[0]+new_list[0])
  53.                             new_list.remove(new_list[0])
  54.                         elif new_list[0].endswith(files_typ[5]) == True:
  55.                             os.rename(start_dir+new_list[0],start_dir+final_dir[1]+new_list[0])
  56.                             new_list.remove(new_list[0])
  57.                         elif new_list[0].endswith(files_typ[6]) == True:
  58.                             os.rename(start_dir+new_list[0],start_dir+final_dir[1]+new_list[0])
  59.                             new_list.remove(new_list[0])
  60.                         elif new_list[0].endswith(files_typ[7]) == True:
  61.                             os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
  62.                             new_list.remove(new_list[0])
  63.                         elif new_list[0].endswith(files_typ[8]) == True:
  64.                             os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
  65.                             new_list.remove(new_list[0])
  66.                         elif new_list[0].endswith(files_typ[9]) == True:
  67.                             os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
  68.                             new_list.remove(new_list[0])
  69.                         elif new_list[0].endswith(files_typ[10]) == True:
  70.                             os.rename(start_dir+new_list[0],start_dir+final_dir[2]+new_list[0])
  71.                             new_list.remove(new_list[0])
  72.                         elif new_list[0].endswith(files_typ[11]) == True:
  73.                             os.rename(start_dir+new_list[0],start_dir+final_dir[3]+new_list[0])
  74.                             new_list.remove(new_list[0])
  75.                         elif new_list[0].endswith(files_typ[12]) == True:
  76.                             os.rename(start_dir+new_list[0],start_dir+final_dir[3]+new_list[0])
  77.                             new_list.remove(new_list[0])
  78.                         elif new_list[0].endswith(files_typ[13]) == True:
  79.                             os.rename(start_dir+new_list[0],start_dir+final_dir[4]+new_list[0])
  80.                             new_list.remove(new_list[0])
  81.                         elif new_list[0].endswith(files_typ[14]) == True:
  82.                             os.rename(start_dir+new_list[0],start_dir+final_dir[4]+new_list[0])
  83.                             new_list.remove(new_list[0])
  84.                         elif new_list[0].endswith(files_typ[15]) == True:
  85.                             os.rename(start_dir+new_list[0],start_dir+final_dir[5]+new_list[0])
  86.                             new_list.remove(new_list[0])
  87.                         elif new_list[0].endswith(files_typ[16]) == True:
  88.                             os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
  89.                             new_list.remove(new_list[0])
  90.                         elif new_list[0].endswith(files_typ[17]) == True:
  91.                             os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
  92.                             new_list.remove(new_list[0])
  93.                         elif new_list[0].endswith(files_typ[18]) == True:
  94.                             os.rename(start_dir+new_list[0],start_dir+final_dir[6]+new_list[0])
  95.                             new_list.remove(new_list[0])
  96.                         elif new_list[0].endswith(files_typ[19]) == True:
  97.                             os.rename(start_dir+new_list[0],start_dir+final_dir[7]+new_list[0])
  98.                             new_list.remove(new_list[0])
  99.                     elif new_list[0].endswith(files_typ[Y]) != True:
  100.                         print files_typ[Y],"does not match",new_list[0]
  101.                         Y = Y + 1
  102.         else:
  103.             os.system("cls")
  104.             print "No more files!"
  105.             X = X + 1
  106.             Z = Z + 1
  107.                
  108.         os.system("cls")
  109.         print "\n No file matches, quitting now!"
  110.         Z = Z + 1
  111.    
  112.     print "\n No files to sort!"
  113.    
  114. os.system("cls")
  115. list_clean()
  116. file_move()
Add Comment
Please, Sign In to add comment