john 1 jaar geleden
bovenliggende
commit
9758f9a388
2 gewijzigde bestanden met toevoegingen van 73 en 28 verwijderingen
  1. 34 0
      delFile.py
  2. 39 28
      diff.py

+ 34 - 0
delFile.py

@@ -0,0 +1,34 @@
+import sqlite3
+import os
+
+# 连接到数据库
+conn = sqlite3.connect('file_data_2.db')
+cursor = conn.cursor()
+
+# 获取要删除的文件名或路径
+file_name_to_delete = 'node_modules'  # 替换为要删除的文件名或路径
+
+# 查询要删除的记录的文件路径
+cursor.execute("SELECT path FROM files WHERE  path like ?", (file_name_to_delete, file_name_to_delete))
+file_paths_to_delete = cursor.fetchall()
+
+# 删除数据库中匹配指定文件名或路径的记录
+cursor.execute("DELETE FROM files WHERE name = ? OR path = ?", (file_name_to_delete, file_name_to_delete))
+deleted_count = cursor.rowcount  # 获取删除的记录数
+
+# 提交更改
+conn.commit()
+
+# 关闭数据库连接
+conn.close()
+
+# 从文件系统中删除相应的文件或目录
+for file_path in file_paths_to_delete:
+    if os.path.exists(file_path[0]):
+        os.remove(file_path[0])  # 或者使用 os.rmdir() 删除目录
+        print(f"已从文件系统中删除: {file_path[0]}")
+    else:
+        print(f"文件或目录不存在: {file_path[0]}")
+
+# 输出删除结果
+print(f"已从数据库中删除 {deleted_count} 条记录,其文件名或路径为: {file_name_to_delete}")

+ 39 - 28
diff.py

@@ -33,37 +33,40 @@ def insert_file_data(directory, ignore_list):
     for root, _, files in os.walk(directory):
         for file in files:
             file_path = os.path.join(root, file)
-            file_name, file_extension = os.path.splitext(file)
-            file_type = file_extension[1:]  # Remove the leading dot from extension
+            if os.path.exists(file_path):  # 检查文件是否存在
+                file_name, file_extension = os.path.splitext(file)
+                file_type = file_extension[1:]  # Remove the leading dot from extension
 
-            # 检查是否在忽略列表中(文件名或目录名)
-            should_ignore = False
-            for ignore_item in ignore_list:
-                if ignore_item in file_path:
-                    should_ignore = True
-                    break
-                if ignore_item in file_type:
-                    should_ignore = True
-                    break
+                # 检查是否在忽略列表中(文件名或目录名)
+                should_ignore = False
+                for ignore_item in ignore_list:
+                    if ignore_item in file_path:
+                        should_ignore = True
+                        break
+                    if ignore_item in file_type:
+                        should_ignore = True
+                        break
 
-            if should_ignore:
-                continue
+                if should_ignore:
+                    continue
 
-            # Get file stats
-            stat_info = os.stat(file_path)
-            size = stat_info.st_size
-            modification_time = stat_info.st_mtime
+                # Get file stats
+                stat_info = os.stat(file_path)
+                size = stat_info.st_size
+                modification_time = stat_info.st_mtime
 
-            # 检查是否存在相同ID
-            cursor.execute('SELECT id FROM files WHERE id=?', (file_path,))
-            existing_id = cursor.fetchone()
+                # 检查是否存在相同ID
+                cursor.execute('SELECT id FROM files WHERE id=?', (file_path,))
+                existing_id = cursor.fetchone()
 
-            if not existing_id:
-                md5_digest = calculate_md5(file_path)
-                cursor.execute(
-                    'INSERT INTO files (id, path, name, type, size, modification_time, md5) VALUES (?, ?, ?, ?, ?, ?, ?)',
-                    (file_path, file_path, file_name, file_type, size, modification_time, md5_digest))
-                conn.commit()
+                if not existing_id:
+                    md5_digest = calculate_md5(file_path)
+                    cursor.execute(
+                        'INSERT INTO files (id, path, name, type, size, modification_time, md5) VALUES (?, ?, ?, ?, ?, ?, ?)',
+                        (file_path, file_path, file_name, file_type, size, modification_time, md5_digest))
+                    conn.commit()
+            else:
+                print(f"文件不存在: {file_path}")
 
 
 ignore_list = [
@@ -71,7 +74,13 @@ ignore_list = [
     '.idea',
     'jar',
     '.git',
-    '.DS_Store'
+    '.DS_Store',
+    'CleanMyMac X.app',
+    '.pnpm-store',
+    'IINA.app',
+    'venv',
+    'python',
+    '/Volumes/16T/newFiles/开放/可执行文件/bin/bit'
 ]
 
 # target_directory = '/path/to/your/directory'
@@ -80,7 +89,9 @@ ignore_list = [
 # target_directory = '/Volumes/20T/待归类'
 # target_directory = '/Volumes/16T/电视剧'
 # target_directory = '/Volumes/16T/电影'
-target_directory = '/Volumes/16T/电子书'
+# target_directory = '/Volumes/16T/电子书'
+# target_directory = '/Volumes/16T/工作'
+target_directory = '/Volumes/16T'
 insert_file_data(target_directory, ignore_list)
 
 # 关闭数据库连接