HylaFAX The world's most advanced open source fax server

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

[hylafax-users] archive faxes in folders by date, script



Hello,
Since i had a need to archive faxes in folders by date i have created
a python script that will do just that. Here it is.

It will take the faxes from the folder and create ./archive folder,
and under that it will create
2007-2-20
2007-2-21 etc...depending on the date modified of file

usage:
python archive.py /var/spool/hylafax/recvq tif
or
python archive.py /home/dept1/faxes pdf

any folder will do, any extension will do.
I'm attaching it in the email or you can download it at a link below:

Enjoy
Lucas

http://www.lucasmanual.com/files/archive.py
"""Lukasz Szybalski, szybalski@xxxxxxxxx, 2-22-2007. This program sorts files and puts them in folder archives/ by date it was modified on. Please provide a folder on a command line, and file extension. Example: python archive.py /department2/faxes tif""" 
import os
import datetime
import sys
import shutil
debuglevel=0

#Get folder name and extension if not specified on the commend line.

def getfolder():
	"""This function asks you for the folder and file extension when programs is called using import archives command"""
	folder=raw_input("Please provide a folder: ")
	extension=raw_input("Please provide a extension: ")
	#See if folder was provided
	try:
		folder
		extension
	except NameError:
		print 'No folder or extension specified'
		print '\nExample:\npython archive /department1/faxes pdf'
	return(folder,extension)
	
#Create a folder inside of an archive folder
def createfolder(foldername):
	"""This function creates a folder inside of the archive folder"""
	#Check if Archive folder exists
	datefolder=os.path.join(folder,'archives/'+foldername)
	if not os.path.exists(os.path.join(folder,datefolder)):
		os.mkdir(os.path.join(folder,datefolder))
		if debuglevel > 0: print >>sys.stderr, 'Creating Archive Folder in: '+str(folder)

#----------------------------------------------------------------------------------------------------
#call this function to check if program was imported or called on a comand line	
if not __name__=="__main__":
	(folder,extension)=getfolder()
else:	
	#See if folder was provided
	try:
		folder=sys.argv[1]
		extension=sys.argv[2]
	except IndexError:
		print 'No folder or extension specified'
		print '\nExample:\npython archive.py /department1/faxes tif'
		sys.exit(1)

#See if folder exists
if os.path.exists(folder):
	if debuglevel > 0: print >>sys.stderr, 'Found Folder: '+str(folder)
else:
	if debuglevel > 0: print >>sys.stderr, 'Folder not Folder: '+folder
	sys.exit(1)


#Check if Archive folder exists
if not os.path.exists(os.path.join(folder,'archives/')):
	os.mkdir(os.path.join(folder,'archives'))
	if debuglevel > 0: print >>sys.stderr, 'Creating Archive Folder in: '+str(folder)

#Global name for archive folder
archivefolder=os.path.join(folder,'archives')


#Get file names in folder
filesinfolder=os.listdir(folder)
if debuglevel > 0: print >>sys.stderr, 'Found: '+str(len(filesinfolder))+' files'

#Create a dictionary with a list of date and filenames with that date.
dateandfile={}

#Go through files, get a date, and create a folder for each date
for file in filesinfolder:
	if file[-len(extension):]==extension:
		filetime=os.stat(os.path.join(folder,file)).st_ctime
		filedate=datetime.date.fromtimestamp(filetime)
		dateandfile[str(filedate)]=[]
		for folderfound in dateandfile:
			createfolder(folderfound)

#Go through files, get a date and append filename to a dictionary dateandfile
for file in filesinfolder:
	if file[-len(extension):]==extension:
		filetime=os.stat(os.path.join(folder,file)).st_ctime
		filedate=datetime.date.fromtimestamp(filetime)
		if debuglevel > 1: print >>sys.stderr, 'Found Date: '+ str(filedate)+' '+ str(file)
		dateandfile[str(filedate)].append(file)

#For each date in the dateandfile dictionary. Go over file names and move each and every one of them to appropriate folders.
for date in dateandfile:
	folderto=os.path.join(archivefolder,date)
	for file in dateandfile[date]:
		shutil.move(os.path.join(folder,file),os.path.join(folderto,file))
		if debuglevel > 1: print >>sys.stderr, 'Moving: ' + os.path.join(folder,file) +' to :'+ os.path.join(folderto,file) 
	

#done. setup this file as a cron job every night at 12:01 am. Enjoy



Project hosted by iFAX Solutions