I have a lot of backup files with names like Corn-Curl-Server_Backup_
that makes sense as [hostname]_[comment]_[date of the backup].
It is easy to scan it with a human eye and discover the backups older than 28 days so they can be deleted, however the files are in separate folders under separate users on the NAS. I can look at any of the folders and delete the ones with an encoded date older than my N value.
I thought I could do something with the date file accessed last but the file dates are not always the dates created based on moving the bunch of them around together and singularly.
I want to parse the date out of each filename and if the date is older than N, I want to move it to colder storage on another volume.

I have a couple of ways to get the timedelta to work, I am most concerned with being able to take filename above and get the date out, turn it into a series that I can load into y, m, d arguments for datetime.date

stale_date = 28
	y = 2012
	m = 1
	d = 23
	datetime.date.today()
	#datetime.date(2012, 2, 14)	
	dd = datetime.date.today() - datetime.date(y, m, d)
	print dd
	
	#datetime.timedelta(31)
	if dd < datetime.timedelta(stale_date):
		print "Young"
	else:
		print "Stale"

Dani AI

Generated

A reliable approach for is to extract an ISO-style date from the filename itself rather than relying on mtime. ’s split-and-strip idea can work, but a small regex that finds a YYYY-MM-DD (or YYYYMMDD) sequence anywhere in the basename is more robust when filenames use multi-dot extensions (like .tar.gz), extra underscores, or extra comments.

import os, re, datetime

_patterns = [re.compile(r'(\d{4})-(\d{2})-(\d{2})'), re.compile(r'(\d{4})(\d{2})(\d{2})')]

def extract_date_from_filename(path):
    base = os.path.basename(path)
    for p in _patterns:
        m = p.search(base)
        if m:
            y, mo, da = map(int, m.groups())
            try:
                return datetime.date(y, mo, da)
            except ValueError:
                return None
    return None

def is_older_than(path, days):
    file_date = extract_date_from_filename(path)
    if file_date is None:
        file_date = datetime.date.fromtimestamp(os.path.getmtime(path))  # fallback
    return (datetime.date.today() - file_date).days > days

Notes and troubleshooting: validate returned dates (catch ValueError), treat future-parsed dates as suspicious, and decide whether to use “> days” or “>= days” to match the intended policy. If filenames sometimes contain multiple date-like tokens, pick the rightmost match (likely the backup date) or refine the regex to anchor near the filename end. For files that don’t contain a parseable date, fall back to mtime or inspect archive metadata. Always run a dry-run log before moving files, and handle permissions and cross-volume moves with shutil.move (or use a copy+verify+delete workflow). This complements the earlier answers and gives a simple, maintainable parser for automated purging or tiering.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.