-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcui.py
More file actions
51 lines (41 loc) · 1.53 KB
/
cui.py
File metadata and controls
51 lines (41 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os
import shutil
import time
from pathlib import Path
# カテゴリ定義
CATEGORY_MAP = {
"ドキュメント": [".pdf", ".doc", ".docx", ".txt", ".xlsx", ".pptx"],
"画像": [".jpg", ".jpeg", ".png", ".gif", ".bmp"],
"動画": [".mp4", ".mov", ".avi", ".mkv"],
"音声": [".mp3", ".wav", ".m4a"],
"アーカイブ": [".zip", ".rar", ".7z"],
"アプリ": [".exe", ".msi"],
}
# フォルダパス(固定)
DOWNLOAD_DIR = Path(r"C:\Users\taiki\Downloads")
SORTED_BASE_DIR = Path(r"C:\Users\taiki\Documents") / "sorted_downloads"
def get_category(extension):
for category, ext_list in CATEGORY_MAP.items():
if extension.lower() in ext_list:
return category
return "その他"
def sort_files_with_move():
for item in DOWNLOAD_DIR.iterdir():
if item.is_file():
category = get_category(item.suffix)
dest_dir = SORTED_BASE_DIR / category
dest_dir.mkdir(parents=True, exist_ok=True)
dest_file = dest_dir / item.name
# 同名ファイルの上書き防止
counter = 1
while dest_file.exists():
dest_file = dest_dir / f"{item.stem} ({counter}){item.suffix}"
counter += 1
try:
shutil.move(str(item), dest_file)
print(f"✔ 移動: {item.name} → {dest_file}")
except Exception as e:
print(f"✘ 移動失敗: {item.name} → {e}")
if __name__ == "__main__":
sort_files_with_move()
time.sleep(10)