#!/usr/bin/python ##################################################################### # AUTHOR: SHELL0CK - https://twitter.com/shell_ock # # AS IS WORKS ON WINDOWS # # THIS PROGRAM AWAITS FOR A NEW MEDIA # # THEN AUTOMATICALLY COPIES SELECTED FILES INTO C:\backup DIRECTORY # #___________________________________________________________________# # # # FIXED OUT OF LOOP # # FIXED HIDDEN FILES COPYING # # FIXED PERCENTEGE COUNT # #___________________________________________________________________# # # ##################################################################### import os import sys import os import datetime from time import gmtime, strftime import time import win32api filename = "C:\\backup\\lista.txt" drives = win32api.GetLogicalDriveStrings() drive = drives.split('\000')[:-1] ListDrives = [] for i in range(len(drive)): print "Found Disk " + drive[i] ListDrives.append(drive[i]) delimiter = len(drive) SYMBOLS = { 'customary' : ('B', 'K', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'), 'customary_ext' : ('byte', 'kilo', 'mega', 'giga', 'tera', 'peta', 'exa', 'zetta', 'iotta'), 'iec' : ('Bi', 'Ki', 'Mi', 'Gi', 'Ti', 'Pi', 'Ei', 'Zi', 'Yi'), 'iec_ext' : ('byte', 'kibi', 'mebi', 'gibi', 'tebi', 'pebi', 'exbi', 'zebi', 'yobi'), } def bytes2human(n, format='%(value).1f %(symbol)s', symbols='customary'): """ Convert n bytes into a human readable string based on format. symbols can be either "customary", "customary_ext", "iec" or "iec_ext", see: https://goo.gl/kTQMs >>> bytes2human(0) '0.0 B' >>> bytes2human(0.9) '0.0 B' >>> bytes2human(1) '1.0 B' >>> bytes2human(1.9) '1.0 B' >>> bytes2human(1024) '1.0 K' >>> bytes2human(1048576) '1.0 M' >>> bytes2human(1099511627776127398123789121) '909.5 Y' >>> bytes2human(9856, symbols="customary") '9.6 K' >>> bytes2human(9856, symbols="customary_ext") '9.6 kilo' >>> bytes2human(9856, symbols="iec") '9.6 Ki' >>> bytes2human(9856, symbols="iec_ext") '9.6 kibi' >>> bytes2human(10000, "%(value).1f %(symbol)s/sec") '9.8 K/sec' >>> # precision can be adjusted by playing with %f operator >>> bytes2human(10000, format="%(value).5f %(symbol)s") '9.76562 K' """ n = int(n) if n < 0: raise ValueError("n < 0") symbols = SYMBOLS[symbols] prefix = {} for i, s in enumerate(symbols[1:]): prefix[s] = 1 << (i+1)*10 for symbol in reversed(symbols[1:]): if n >= prefix[symbol]: value = float(n) / prefix[symbol] return format % locals() return format % dict(symbol=symbols[0], value=n) def human2bytes(s): init = s num = "" while s and s[0:1].isdigit() or s[0:1] == '.': num += s[0] s = s[1:] num = float(num) letter = s.strip() for name, sset in SYMBOLS.items(): if letter in sset: break else: if letter == 'k': # treat 'k' as an alias for 'K' as per: https://goo.gl/kTQMs sset = SYMBOLS['customary'] letter = letter.upper() else: raise ValueError("can't interpret %r" % init) prefix = {sset[0]:1} for i, s in enumerate(sset[1:]): prefix[s] = 1 << (i+1)*10 return int(num * prefix[letter]) if not os.path.isdir("c:\\backup"): cmd = "mkdir c:\\backup" os.system(cmd) print "Directory Set" if os.path.isfile(filename): target = open(filename, 'w') target.truncate() else: touch = "echo $null >> " + filename os.system(touch) target = open(filename, 'w') target.truncate() def main(interestDrive) : import fnmatch TotalSize = 0 matches = [] matchesSize = [] for root, dirs, files in os.walk(interestDrive): for file in files: if file.endswith('.doc') or file.endswith('.docx')or file.endswith('.mp4') or file.endswith('.jpg') or file.endswith('.MTS') or file.endswith('.jpeg') or file.endswith('.m4a') or file.endswith('.AMR') or file.endswith('.amr'): matches.append(os.path.join(root, file)) path = os.path.join(root, file) size = os.stat(path).st_size matchesSize.append(size) TotalSize = TotalSize + size size1 = bytes2human(size) #print file + " ___ " + str(size1) counter = 0 #print TotalSize TotalSize1 = bytes2human(TotalSize) #print TotalSize1 copiedSize = 0 while counter +1 < len(matches): TotalcopiedSize = 0 for m in matches: discriminant = m.split("\\") discriminant1 = discriminant[0] + "\\" + discriminant[1] GetSize = matchesSize[counter] sizeFile = bytes2human(GetSize) sizeFile = str(sizeFile) def completitionPercent(p,t): per1 =(p*100)/t return float(per1) TotalcopiedSize = TotalcopiedSize + GetSize per = completitionPercent(TotalcopiedSize,TotalSize) progress = str(per) + "%" sys.stdout.write("Download progress: %d%% \r" % (per) ) sys.stdout.flush() #print str(TotalcopiedSize) + " -- " +str(TotalSize) if TotalcopiedSize + 1 < TotalSize: now = strftime("%d-%m-%Y %H:%M:%S") #below final command to copy also hidden files (/H switch) and redirect output to null "" --> Double quotes are used to fix error due to spaces between files names cmd = "xcopy /H /Y " + "\"" + m + "\"" + " \"C:\\backup\" > nul 2>&1" #cmd = "xcopy /H " + "\"" + m + "\"" + " \"C:\\backup\"" ex = "The system cannot find the file specified." try: #print " copying " + m os.system(cmd) os.devnull target.write("[" + str(counter) + "] SUCCESS - " + m + " @ " + now + " --- " + str(sizeFile) + "\n") counter = counter +1 print "copied " + str(GetSize) + " of " + str(TotalSize) + " --- " + str(sizeFile) + " @ " + now + "\n" except Exception as mi: print mi print "could not copy " + m target.write(mi) target.write("\n[" + str(counter) + "] FAILED - " + m + " @ " + now + "\n") target.write("\n\n\n\n\n") counter = counter +1 else: now = strftime("%d-%m-%Y %H:%M:%S") print "\nAll DONE )" target.write(" COPYING FINISHED @ " + now + " --- " + str(TotalSize1) + "\n") #below to create hidden.txt containig all hidden file list cmd = "dir c:\\backup /S /A:H > c:\\backup\\hidden.txt" os.system(cmd) stopper = i+1 if len(drive) == stopper : os.system("calc.exe") sys.exit() target.close() while True: drives = win32api.GetLogicalDriveStrings() drive = drives.split('\000')[:-1] time.sleep(1) ListDrives2 =[] if len(drive) > delimiter: for i in range(len(drive)): ListDrives2.append(drive[i]) for d in ListDrives2: if d not in ListDrives: print "Found Disk " + d interestDrive = d break main(interestDrive)