ntb
This commit is contained in:
3
.idea/.gitignore
generated
vendored
Normal file
3
.idea/.gitignore
generated
vendored
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
# Default ignored files
|
||||||
|
/shelf/
|
||||||
|
/workspace.xml
|
||||||
10
.idea/WalkFiles.iml
generated
Normal file
10
.idea/WalkFiles.iml
generated
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<module type="PYTHON_MODULE" version="4">
|
||||||
|
<component name="NewModuleRootManager">
|
||||||
|
<content url="file://$MODULE_DIR$">
|
||||||
|
<excludeFolder url="file://$MODULE_DIR$/.venv" />
|
||||||
|
</content>
|
||||||
|
<orderEntry type="jdk" jdkName="Python 3.12 (WalkFiles)" jdkType="Python SDK" />
|
||||||
|
<orderEntry type="sourceFolder" forTests="false" />
|
||||||
|
</component>
|
||||||
|
</module>
|
||||||
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
6
.idea/inspectionProfiles/profiles_settings.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<component name="InspectionProjectProfileManager">
|
||||||
|
<settings>
|
||||||
|
<option name="USE_PROJECT_PROFILE" value="false" />
|
||||||
|
<version value="1.0" />
|
||||||
|
</settings>
|
||||||
|
</component>
|
||||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="ProjectModuleManager">
|
||||||
|
<modules>
|
||||||
|
<module fileurl="file://$PROJECT_DIR$/.idea/WalkFiles.iml" filepath="$PROJECT_DIR$/.idea/WalkFiles.iml" />
|
||||||
|
</modules>
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project version="4">
|
||||||
|
<component name="VcsDirectoryMappings">
|
||||||
|
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||||
|
</component>
|
||||||
|
</project>
|
||||||
40
10 WalkFiles.py
Normal file
40
10 WalkFiles.py
Normal file
@@ -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)}")
|
||||||
106
20 Walkandsave.py
Normal file
106
20 Walkandsave.py
Normal file
@@ -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'.")
|
||||||
6
credentials.env
Normal file
6
credentials.env
Normal file
@@ -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
|
||||||
|
|
||||||
Reference in New Issue
Block a user