htnawsaj

CleanDownloads

Jan 27th, 2014
321
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.58 KB | None | 0 0
  1. import os
  2. import shutil
  3. import time
  4.  
  5.  
  6. #The script moves files from the source location to destination according to file types defined
  7. #assumes that the destination paths and folders are preexisting
  8.  
  9.  
  10. #define the folders to be cleaned
  11. source = ['C:\Users\xxxx\Downloads','C:\Users\xxxx\Desktop']
  12.  
  13. #define the folder location for the files to be moved
  14. jpgpath = 'C:\Users\xxxx\Downloads\_Jpg'
  15. zippath = 'C:\Users\xxxx\Downloads\_Zipfiles'
  16. mediapath = 'C:\Users\xxxx\Downloads\_Media'
  17. docpath = 'C:\Users\xxxx\Downloads\_docs'
  18.  
  19. #use the above locations for archiving or change if needed.
  20. archivepaths = [jpgpath,zippath,mediapath,docpath]
  21. filestomove = []
  22.  
  23. # time period to Archive
  24. now = time.time()
  25. week_ago = now - 60*60*24*7
  26.  
  27. def copyfile(paths):
  28.     for item in paths:
  29. ##        print file
  30.         if getdestination(item):
  31.             try:
  32.                 shutil.move(item,getdestination(item))
  33.                 print "Moved" + str(item)
  34.             except Exception as e:
  35.                 print e
  36.                 print str(item) + 'failed to move'
  37.  
  38.  
  39.  
  40. def checkcreateddate(item):
  41.     modified_date = os.path.getmtime(item)
  42.     if modified_date < week_ago:
  43.         print str(item) + "is older" + "\n"
  44.         return True
  45.     else:
  46.         print str(item) + "is new" + "\n"
  47.         return False
  48.  
  49. def getfileslist(dir):
  50.     fileswithpath = []
  51.     files = os.listdir(dir)
  52.     for item in files:
  53.         fileswithpath.append(dir +'/'+ item)
  54.     return fileswithpath
  55.  
  56. def getdestination(item):
  57.         if '.jpg' in item or '.JPG' in item :
  58.             return jpgpath
  59.         elif '.png' in item:
  60.             return jpgpath
  61.         elif '.docx' in item or '.xlsx' in item or '.doc' in item or '.rtf' in item or '.txt' in item or '.xls' in item:
  62.             return docpath
  63.         elif '.zip' in item :
  64.             return zippath
  65.         elif '.mp4' in item or '.avi' in item or '.flv' in item:
  66.             return mediapath
  67.         else:
  68.             return False
  69.  
  70. def archivefile(file):
  71.     for item in file:
  72.         if getdestination(item):
  73.             try:
  74.                 if checkcreateddate(item):
  75.                     shutil.move(item,getdestination(item)+'\Archive')
  76.                     print str(item) + 'has been archived'
  77.             except Exception as e:
  78.                 print e
  79.                 print str(item) + 'failed to archive'
  80.  
  81. #Cleanup items
  82.  
  83. for item in source:
  84.     filestomove = getfileslist(item)
  85.     copyfile(filestomove)
  86.  
  87. #Archive items
  88.  
  89. for item in archivepaths:
  90.     filestomove = getfileslist(item)
  91.     archivefile(filestomove)
Add Comment
Please, Sign In to add comment