How to Build a Document Change Tracking and Alert System
Get alerts when important documents are modified or updated.
Jay Banlasan
The AI Systems Guy
When a critical document changes and nobody notices, things break downstream. I built a document change tracking alert system that watches important files and sends notifications the moment something gets modified. No more discovering changes three days late.
This works for contracts, SOPs, client briefs, or any file where unauthorized or unnoticed edits cause problems.
What You Need Before Starting
- Python 3.8+
- Access to your file storage (local, Google Drive, or S3)
- A notification channel (Slack, email, or both)
- A database for tracking file states (SQLite works fine)
Step 1: Set Up the Tracking Database
import sqlite3
from pathlib import Path
def init_db(db_path="document_tracker.db"):
conn = sqlite3.connect(db_path)
conn.execute("""
CREATE TABLE IF NOT EXISTS tracked_files (
file_path TEXT PRIMARY KEY,
last_hash TEXT,
last_modified TEXT,
last_checked TEXT
)
""")
conn.commit()
return conn
Step 2: Hash Files for Change Detection
Use SHA-256 to detect any change, no matter how small:
import hashlib
def get_file_hash(file_path):
hasher = hashlib.sha256()
with open(file_path, "rb") as f:
for chunk in iter(lambda: f.read(4096), b""):
hasher.update(chunk)
return hasher.hexdigest()
Step 3: Build the Change Scanner
from datetime import datetime
def scan_for_changes(conn, watch_dirs):
changes = []
cursor = conn.cursor()
for directory in watch_dirs:
for filepath in Path(directory).rglob("*"):
if filepath.is_file():
current_hash = get_file_hash(filepath)
str_path = str(filepath)
cursor.execute(
"SELECT last_hash FROM tracked_files WHERE file_path = ?",
(str_path,)
)
row = cursor.fetchone()
if row is None:
cursor.execute(
"INSERT INTO tracked_files VALUES (?, ?, ?, ?)",
(str_path, current_hash,
filepath.stat().st_mtime, datetime.now().isoformat())
)
elif row[0] != current_hash:
changes.append({
"file": str_path,
"old_hash": row[0],
"new_hash": current_hash,
"detected_at": datetime.now().isoformat()
})
cursor.execute(
"UPDATE tracked_files SET last_hash=?, last_checked=? WHERE file_path=?",
(current_hash, datetime.now().isoformat(), str_path)
)
conn.commit()
return changes
Step 4: Send Alert Notifications
import requests
def send_slack_alert(changes, webhook_url):
if not changes:
return
blocks = [{"type": "header", "text": {
"type": "plain_text",
"text": f"{len(changes)} Document(s) Changed"
}}]
for change in changes:
blocks.append({
"type": "section",
"text": {
"type": "mrkdwn",
"text": f"*{Path(change['file']).name}*\nDetected: {change['detected_at']}"
}
})
requests.post(webhook_url, json={"blocks": blocks})
Step 5: Schedule the Scanner
Run the scan every 15 minutes with a cron job or a simple loop:
# Add to crontab
*/15 * * * * python3 /path/to/document_scanner.py
Or run it as a persistent watcher for near-instant detection using the watchdog library from system 298.
What to Build Next
Add version snapshots so you can see exactly what changed, not just that something changed. Store diffs alongside the alerts. Or add role-based alerts where certain file changes only notify specific people.
Related Reading
- The Automation Decision Tree - automation decision tree framework
- Building Your First Automation: A Complete Guide - first automation guide business
- The Automation Myth - automation myth business
Want this system built for your business?
Get a free assessment. We will map every system your business needs and show you the ROI.
Get Your Free Assessment