import os import sys import datetime import mysql.connector # 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. # Version 1.1 # Changes: # - Updated table name to be configurable via DB_CONFIG # 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": "folders" } # =================================================== TABLE_SQL = """ CREATE TABLE IF NOT EXISTS {table} ( 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 {table} (name, last_modified, active) VALUES (%s, %s, 1) ON DUPLICATE KEY UPDATE last_modified = VALUES(last_modified); """ MARK_INACTIVE_SQL_TEMPLATE = "UPDATE {table} SET active = 0 WHERE name NOT IN ({placeholders});" MARK_ALL_INACTIVE_SQL = "UPDATE {table} SET active = 0;" def get_connection(): # Exclure la clé 'table' des paramètres de connexion conn_params = {k: v for k, v in DB_CONFIG.items() if k != "table"} return mysql.connector.connect(**conn_params) def ensure_table(cursor): # Formatter le SQL de création avec le nom de table configuré cursor.execute(TABLE_SQL.format(table=DB_CONFIG["table"])) 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: # Formatter l'UPSERT avec le nom de table cur.execute(UPSERT_SQL.format( table=DB_CONFIG["table"]), (name, mtime)) if current_names: placeholders = ",".join(["%s"] * len(current_names)) # Formatter la requête de mise à jour avec le nom de table sql = MARK_INACTIVE_SQL_TEMPLATE.format( table=DB_CONFIG["table"], placeholders=placeholders) cur.execute(sql, list(current_names)) else: cur.execute(MARK_ALL_INACTIVE_SQL.format(table=DB_CONFIG["table"])) 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()