Files
python-folder-to-mysql/FoldersToMySQL.py

120 lines
3.1 KiB
Python

import os
import sys
import datetime
import mysql.connector
# Version 1.0
# Author: Sébastien Plante
# Date: 2025-10-07
# Description:
# This script scans a specified directory for folders and synchronizes their names and last modified dates
# with a MySQL database. It marks folders as inactive if they are no longer present in the directory.
# Requirements:
# Install python 3.x from https://www.python.org/downloads/
# pip install mysql-connector-python
# Usage:
# python foldersToSQL.py
# Optionally, you can specify a different path to scan:
# python foldersToSQL.py "C:\another folder"
# ================== CONFIGURATION ==================
SCAN_PATH = r"C:\test folder"
DB_CONFIG = {
"host": "localhost",
"user": "root",
"password": "qwerty1234",
"database": "testdb",
}
# ===================================================
TABLE_SQL = """
CREATE TABLE IF NOT EXISTS folders (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL UNIQUE,
last_modified DATETIME NOT NULL,
active TINYINT(1) NOT NULL DEFAULT 1
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""
UPSERT_SQL = """
INSERT INTO folders (name, last_modified, active)
VALUES (%s, %s, 1)
ON DUPLICATE KEY UPDATE
last_modified = VALUES(last_modified);
"""
MARK_INACTIVE_SQL_TEMPLATE = "UPDATE folders SET active = 0 WHERE name NOT IN ({placeholders});"
MARK_ALL_INACTIVE_SQL = "UPDATE folders SET active = 0;"
def get_connection():
return mysql.connector.connect(**DB_CONFIG)
def ensure_table(cursor):
cursor.execute(TABLE_SQL)
def list_directories(base_path):
try:
entries = os.listdir(base_path)
except OSError as e:
print(f"Error reading directory: {e}")
sys.exit(1)
dirs = []
for name in entries:
full = os.path.join(base_path, name)
if os.path.isdir(full):
mtime = datetime.datetime.fromtimestamp(os.path.getmtime(full))
dirs.append((name, mtime))
return dirs
def sync(base_path):
conn = get_connection()
try:
cur = conn.cursor()
ensure_table(cur)
current_dirs = list_directories(base_path)
current_names = {name for name, _ in current_dirs}
for name, mtime in current_dirs:
cur.execute(UPSERT_SQL, (name, mtime))
if current_names:
placeholders = ",".join(["%s"] * len(current_names))
sql = MARK_INACTIVE_SQL_TEMPLATE.format(placeholders=placeholders)
cur.execute(sql, list(current_names))
else:
cur.execute(MARK_ALL_INACTIVE_SQL)
conn.commit()
print(
f"Synchronization complete. Active folders: {len(current_dirs)}")
except mysql.connector.Error as err:
print(f"MySQL error: {err}")
finally:
cur.close()
conn.close()
def main():
if len(sys.argv) >= 2:
base_path = sys.argv[1]
else:
base_path = SCAN_PATH
if not os.path.isdir(base_path):
print(f"Invalid path: {base_path}")
sys.exit(1)
print(f"Scanning directory: {base_path}")
sync(base_path)
if __name__ == "__main__":
main()