From f41b2c43bc06dc6a3cf01e6978de04a89ea7b6c7 Mon Sep 17 00:00:00 2001 From: Vladimir Buzalka Date: Sun, 23 Nov 2025 17:53:47 +0100 Subject: [PATCH] ntb --- .idea/.gitignore | 3 + .idea/WalkFiles.iml | 10 ++ .../inspectionProfiles/profiles_settings.xml | 6 + .idea/modules.xml | 8 ++ .idea/vcs.xml | 6 + 10 WalkFiles.py | 40 +++++++ 20 Walkandsave.py | 106 ++++++++++++++++++ credentials.env | 6 + 8 files changed, 185 insertions(+) create mode 100644 .idea/.gitignore create mode 100644 .idea/WalkFiles.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 10 WalkFiles.py create mode 100644 20 Walkandsave.py create mode 100644 credentials.env diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..26d3352 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,3 @@ +# Default ignored files +/shelf/ +/workspace.xml diff --git a/.idea/WalkFiles.iml b/.idea/WalkFiles.iml new file mode 100644 index 0000000..6a0acd3 --- /dev/null +++ b/.idea/WalkFiles.iml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..9801575 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/10 WalkFiles.py b/10 WalkFiles.py new file mode 100644 index 0000000..cb2cb1b --- /dev/null +++ b/10 WalkFiles.py @@ -0,0 +1,40 @@ +import os +from datetime import datetime + + + +def walk_files(target_dir): + file_data = [] + for root, dirs, files in os.walk(target_dir): + for file in files: + file_path = os.path.join(root, file) + try: + stats = os.stat(file_path) + file_info = { + 'path': file_path, + 'name': file, + 'size': stats.st_size, + 'modified': datetime.fromtimestamp(stats.st_mtime).strftime('%Y-%m-%d %H:%M:%S'), + 'type': os.path.splitext(file)[1] + } + file_data.append(file_info) + except FileNotFoundError: + continue + return file_data + + +def print_files(file_data): + print(f"{'Name':40} {'Size (bytes)':>15} {'Modified':>20} {'Type':>10}") + print('-' * 90) + for f in file_data: + print(f"{f['name'][:38]:40} {f['size']:>15} {f['modified']:>20} {f['type']:>10}") + + +if __name__ == '__main__': + target = "u:\Dropbox\Ordinace\Dokumentace_ke_zpracování" + if not os.path.isdir(target): + print("Invalid directory. Please try again.") + else: + results = walk_files(target) + print_files(results) + print(f"\nTotal files found: {len(results)}") diff --git a/20 Walkandsave.py b/20 Walkandsave.py new file mode 100644 index 0000000..791e0d0 --- /dev/null +++ b/20 Walkandsave.py @@ -0,0 +1,106 @@ +import os +import mysql.connector +from datetime import datetime +from dotenv import load_dotenv + +load_dotenv() # Reads .env file and adds to environment + +# Database setup with explicit UTF8MB4 collation +def init_db(): + conn = mysql.connector.connect( + host=os.getenv("DB_MYSQL_HOST"), + user=os.getenv("DB_MYSQL_ROOT"), + password=os.getenv("DB_MYSQL_ROOT_PASS"), + database=os.getenv("walkfiles"), + port=int(os.getenv("DB_MYSQL_PORT", 3306)), + charset="utf8mb4", + collation="utf8mb4_general_ci" + ) + cursor = conn.cursor() + cursor.execute("CREATE DATABASE IF NOT EXISTS walkfiles CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci") + cursor.execute("USE walkfiles") + + cursor.execute('''CREATE TABLE IF NOT EXISTS devices ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci UNIQUE, + scanned_at DATETIME + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci''') + + cursor.execute('''CREATE TABLE IF NOT EXISTS folders ( + id INT AUTO_INCREMENT PRIMARY KEY, + path TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci UNIQUE, + parent_id INT, + device_id INT, + FOREIGN KEY(device_id) REFERENCES devices(id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci''') + + cursor.execute('''CREATE TABLE IF NOT EXISTS files ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, + path TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci UNIQUE, + size BIGINT, + modified DATETIME, + type VARCHAR(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci, + folder_id INT, + device_id INT, + FOREIGN KEY(folder_id) REFERENCES folders(id), + FOREIGN KEY(device_id) REFERENCES devices(id) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci''') + + conn.commit() + return conn, cursor + + +def insert_bulk_files(cursor, conn, files_data): + if not files_data: + return + query = '''INSERT IGNORE INTO files (name, path, size, modified, type, folder_id, device_id) + VALUES (%s,%s,%s,%s,%s,%s,%s)''' + cursor.executemany(query, files_data) + conn.commit() + + +def walk_and_store_bulk(): + target_dir = r"u:\\Dropbox\\Ordinace\\Dokumentace_ke_zpracování" + device_name = "NTB" + conn, cursor = init_db() + now = datetime.now() + + cursor.execute("INSERT IGNORE INTO devices (name, scanned_at) VALUES (%s, %s)", (device_name, now)) + conn.commit() + cursor.execute("SELECT id FROM devices WHERE name=%s", (device_name,)) + device_id = cursor.fetchone()[0] + + folder_cache = {} + files_to_insert = [] + + for root, dirs, files in os.walk(target_dir): + parent_path = os.path.dirname(root) + parent_id = folder_cache.get(parent_path) + + cursor.execute("INSERT IGNORE INTO folders (path, parent_id, device_id) VALUES (%s, %s, %s)", (root, parent_id, device_id)) + conn.commit() + cursor.execute("SELECT id FROM folders WHERE path=%s", (root,)) + folder_id = cursor.fetchone()[0] + folder_cache[root] = folder_id + + for file in files: + file_path = os.path.join(root, file) + try: + stats = os.stat(file_path) + modified = datetime.fromtimestamp(stats.st_mtime) + ftype = os.path.splitext(file)[1] + files_to_insert.append((file, file_path, stats.st_size, modified, ftype, folder_id, device_id)) + except FileNotFoundError: + continue + + insert_bulk_files(cursor, conn, files_to_insert) + conn.close() + + +if __name__ == '__main__': + if not os.path.isdir(r"u:\\Dropbox\\Ordinace\\Dokumentace_ke_zpracování"): + print("Invalid directory path.") + else: + walk_and_store_bulk() + print("Scan completed for directory 'u:\\Dropbox\\Ordinace\\Dokumentace_ke_zpracování' on device 'NTB'. Bulk data stored efficiently in MySQL database 'walkfiles'.") \ No newline at end of file diff --git a/credentials.env b/credentials.env new file mode 100644 index 0000000..4e6ec74 --- /dev/null +++ b/credentials.env @@ -0,0 +1,6 @@ +DB_MYSQL_HOST=192.168.1.76 +DB_MYSQL_PORT=3307 +DB_MYSQL_ROOT=root +DB_MYSQL_ROOT_PASS=Vlado9674+ +DB_MYSQL_PORT=3307 +