import os, sys from datetime import datetime import winsys TARGET = r"c:\temp\archive" ROBOCOPY = r'robocopy.exe "%s" "%s\%s\%s" /S /move /copyall /minage:%s1231' def min_datestamp (root): current_min = datetime.max for dirpath, dirs, files in winsys.walk (root): current_min = min ([current_min] + [f.accessed_at for f in files]) return current_min def main (root): if not os.path.isdir (root): raise RuntimeError, "Root must be a folder, not a file" source = os.path.basename (root) oldest = min_datestamp (root) print "Oldest file last touched at", oldest min_year = oldest.year for year in range (min_year, datetime.now ().year): command = ROBOCOPY % (root, TARGET, source, year, year) print command os.system (command) if __name__ == '__main__': main (sys.argv[1])