user
pass
remember me
 
Gia to nomosxedeio
2017-10-09 21:58
Pota
2017-05-31 13:30
Gati
2017-04-03 15:24
 
from thk thk on 2016-11-14 15:18
1 1 votes
wait..Voting.. bad good
If you have a folder that contains thousands of files (in my case it was the motion daemon folder that contained thousands of pictures and videos from a surveillance camera) this script will organize them in sub folders named by the last modification date.

import os 
import sys
import time
from datetime import datetime
import shutil

def orgbyday(directory):
    ans=raw_input("Going to organize files in directory : " + directory  +  " by  modification day in subfolders.Type 'y' to continue:")
    if ans != 'y':
print 'exiting'
sys.exit(0)
    print "listing dir: " + directory
    l = [file for file in os.listdir(directory) 
         if os.path.isfile(os.path.join(directory, file)) ] 
   
    ilen = len(l)
    slen = (str(ilen))
    print "found " + slen + " files"
    i=1
    startt= time.time()
    for f in l:
 
percent = (float(i) / float(ilen)) * 100.0
spercent = "%.2f" % percent
   
print str(i) + " of " + slen 
print spercent + " %  "
passed=time.time()-startt
remaining = (ilen  - i) * (passed / i)  
m, s = divmod(remaining, 60)
h, m = divmod(m, 60)
print "%d:%02d:%02d" % (h, m, s)
#print str(remaining)
print f
ff=os.path.join(directory,f)
mtime = os.path.getmtime(ff)
print "modified: %s" % time.ctime(mtime) 
mdate = datetime.fromtimestamp(mtime)
creation_day = mdate.strftime('%Y-%m-%d')
subfolder = os.path.join(directory,creation_day )
dest = os.path.join(subfolder , f )
try:
   shutil.move (ff , dest)
except:
   os.mkdir(subfolder)  
   shutil.move (ff , dest)
print "                               "
print " "
print "\x1b[2J\x1b[H"
i +=1
 
if __name__ == '__main__':
    orgbyday(sys.argv[1])