Skip to content

Instantly share code, notes, and snippets.

@Grimthorr
Created December 19, 2014 14:36
Show Gist options
  • Save Grimthorr/13bd1a7f802b3111d67b to your computer and use it in GitHub Desktop.
Save Grimthorr/13bd1a7f802b3111d67b to your computer and use it in GitHub Desktop.
Python script to move old files from one folder to another.
import sys
import os
import glob
import shutil
import time
import logging
from datetime import datetime, date, timedelta
# start editable vars #
days_old = 28 # how old the files have to be before they are moved
original_folder = "/source/folder" # folder to move files from
new_folder = "/target/folder" # folder to move files to
logfile = "/somewhere/log.log" # log file to record what has happened
# end editable vars #
# start function definitions #
def log(level,msg,tofile=True):
print msg
if tofile == True:
if level == 0:
logger.info(msg)
else:
logger.error(msg)
def end(code):
log(0,"End.")
log(0,"-------------------------")
sys.exit(code)
# end function definitions #
# start process #
move_date = date.today() - timedelta(days=days_old)
move_date = time.mktime(move_date.timetuple())
logger = logging.getLogger("cuarch")
hdlr = logging.FileHandler(logfile)
hdlr.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s: %(message)s'))
logger.addHandler(hdlr)
logger.setLevel(logging.INFO)
log(0,"Initialising...")
count = 0
size = 0.0
for filename in glob.glob1(original_folder, "*.*"):
srcfile = os.path.join(original_folder, filename)
destfile = os.path.join(new_folder, filename)
if os.stat(srcfile).st_mtime < move_date:
if not os.path.isfile(destfile):
size = size + (os.path.getsize(srcfile) / (1024*1024.0))
shutil.move(srcfile, destfile)
log(0,"Archived '" + filename + "'.")
count = count + 1
log(0,"Archived " + str(count) + " files, totalling " + str(round(size,2)) + "MB.")
end(0)
@grantpasley
Copy link

thanks for this, how to move sub directories within the src directory as well please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment