Skip to content

Instantly share code, notes, and snippets.

@hmms
Last active August 17, 2017 21:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hmms/4eb71cc2bc212d3e54e5d74966bebcfd to your computer and use it in GitHub Desktop.
Save hmms/4eb71cc2bc212d3e54e5d74966bebcfd to your computer and use it in GitHub Desktop.
Python 3 script to move files and folders from desktop(and other directories) for archiving. Automates organization of the data that you work on by time, tested on Win 10 Pro. Script can be triggered to run autonomously through Windows Task Scheduler
#File Archiver
#Murli Shenoy - August 2017
#License - please keep this header intact, reuse/modify the code as you wish
import shutil
import datetime as dt
import time
import ctypes, sys, os, traceback, types
FILE_SIZE_LIMIT = 30720000 # Approx 30Mb limit
# set the date limits
now = dt.datetime.now()
ago = now - dt.timedelta(hours=24)#archive everything that's less than 24 hours old
scanned_dirs = [r'C:\Users\hmms\Desktop', r'C:\Users\hmms\Desktop\Temp',r'C:\Users\hmms\Downloads'] # add directories here
dest_dir = r'C:\Users\hmms\Documents' # this is the destination directory for archiving
# create direcotory
daily_dir_name = os.path.abspath(dest_dir + "\\" + time.strftime("%d%m%Y"))
if not os.path.exists(daily_dir_name):
os.makedirs(daily_dir_name)
# pick a folder you have ...
def get_folder_size(folder):
folder_size = 0
for (path, dirs, files) in os.walk(folder):
for file in files:
filename = os.path.join(path, file)
folder_size += os.path.getsize(filename)
return folder_size/(1.024*1.024)
#thanks to Martin De la Fuente on Stackoverflow
#link: https://stackoverflow.com/questions/130763/request-uac-elevation-from-within-a-python-script
def is_admin():
try:
return ctypes.windll.shell32.IsUserAnAdmin()
except:
return False
#all the magic happens here
def file_archiver():
for current_dir in scanned_dirs:
file_list = os.listdir(os.path.abspath(current_dir))
for source_file_name_to_be_moved in file_list:
current_file_loc = os.path.abspath(current_dir + "\\" + source_file_name_to_be_moved) # current file location
st = os.stat(current_file_loc)
ModifyTime = dt.datetime.fromtimestamp(st.st_ctime)
FileSize = st.st_size
if ModifyTime > ago: # Archive files created/modified yesterday
print ("here")
print (current_file_loc)
if FileSize < FILE_SIZE_LIMIT: # We don't want to archive really BIG files
print(current_file_loc)
source_filename = os.path.abspath(current_dir + "\\" + source_file_name_to_be_moved)
print("source file name")
print(source_filename)
if os.path.isfile(os.path.abspath(source_filename)):#copy only files, os.walk?
shutil.move(source_filename, daily_dir_name)
else: # move directories after checking dir size, TODO: maybe walk inside the dir and move only the new files?
DirSize = get_folder_size(source_filename)
if (DirSize < FILE_SIZE_LIMIT):
shutil.move(source_filename, daily_dir_name)
if is_admin():
#print('file_archiver')
file_archiver()
else:
# Re-run the program with admin rights
ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, "", None, 1)
#print('file_archiver admin')
file_archiver()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment