Skip to content

Instantly share code, notes, and snippets.

@DagW
Last active June 1, 2017 19:22
Show Gist options
  • Save DagW/91dad29cd0560e9a8a4fb80d02aca0b6 to your computer and use it in GitHub Desktop.
Save DagW/91dad29cd0560e9a8a4fb80d02aca0b6 to your computer and use it in GitHub Desktop.
Archive files in fast growing directories, group a days file to a zip etc. Run via cron or taskscheduler
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import with_statement
from contextlib import closing
from zipfile import ZipFile, ZIP_DEFLATED
import os
import datetime
import shutil
def zipdir(basedir, archivename):
assert os.path.isdir(basedir)
with closing(ZipFile(archivename, "w", ZIP_DEFLATED)) as z:
for root, dirs, files in os.walk(basedir):
#NOTE: ignore empty directories
for fn in files:
absfn = os.path.join(root, fn)
zfn = absfn[len(basedir)+len(os.sep):] #XXX: relative path
z.write(absfn, zfn)
def archiveToZipByMonth(dir_to_search, file_type):
source = os.listdir(dir_to_search)
for files in source:
#Get only our filetypes
if files.endswith("."+file_type):
curpath = os.path.join(dir_to_search, files)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
filedate = str(file_modified.year)+"-"+str(file_modified.month)
destinationdir = dir_to_search+"\\"+filedate+"\\"
destinationfile = dir_to_search+"\\"+filedate+"\\"+files
#Do not move if it is this day
if not filedate == str(datetime.date.today().year)+"-"+str(datetime.date.today().month):
#Create dir if not exists
if not os.path.exists(destinationdir):
os.mkdir(destinationdir)
if os.path.exists(destinationfile):
os.remove(destinationfile)
shutil.move(dir_to_search+"\\"+files,destinationdir)
source = os.listdir(dir_to_search)
for files in source:
if os.path.isdir(dir_to_search+"\\"+files):
if not files == str(datetime.date.today().year)+"-"+str(datetime.date.today().month):
destinationzip = dir_to_search+"\\"+files+".zip"
if not os.path.exists(destinationzip):
zipdir(dir_to_search+"\\"+files, destinationzip)
shutil.rmtree(dir_to_search+"\\"+files)
def archiveToZipByDay(dir_to_search, file_type):
source = os.listdir(dir_to_search)
for files in source:
#Get only our filetypes
if files.endswith("."+file_type):
curpath = os.path.join(dir_to_search, files)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
filedate = str(file_modified.year)+"-"+str(file_modified.month)+"-"+str(file_modified.day)
destinationdir = dir_to_search+"\\"+filedate+"\\"
destinationfile = dir_to_search+"\\"+filedate+"\\"+files
#Do not move if it is this day
if not filedate == str(datetime.date.today().year)+"-"+str(datetime.date.today().month)+"-"+str(datetime.date.today().day):
#Create dir if not exists
if not os.path.exists(destinationdir):
os.mkdir(destinationdir)
if os.path.exists(destinationfile):
os.remove(destinationfile)
shutil.move(dir_to_search+"\\"+files,destinationdir)
source = os.listdir(dir_to_search)
for files in source:
if os.path.isdir(dir_to_search+"\\"+files):
if not files == str(datetime.date.today().year)+"-"+str(datetime.date.today().month)+"-"+str(datetime.date.today().day):
destinationzip = dir_to_search+"\\"+files+".zip"
if not os.path.exists(destinationzip):
zipdir(dir_to_search+"\\"+files, destinationzip)
shutil.rmtree(dir_to_search+"\\"+files)
def removeZipsOlderThan(dir_to_search, num_days):
source = os.listdir(dir_to_search)
for files in source:
#Get only our filetypes
if files.endswith(".zip"):
curpath = os.path.join(dir_to_search, files)
file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath))
if datetime.datetime.now() - file_modified > datetime.timedelta(days=num_days):
os.remove(curpath)
print("removed file")
pass
archiveToZipByDay("C:\\rotate", "rtf")
removeZipsOlderThan("C:\\rotate", 90)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment