-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfiles_and_folders.py
More file actions
123 lines (93 loc) · 3.6 KB
/
files_and_folders.py
File metadata and controls
123 lines (93 loc) · 3.6 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import os
import shutil
import re
#Choose Directoy
base_dir = os.path.dirname(os.path.abspath(__file__))
#base_dir = r"C:\Users\USERNAME\Downloads"
def fix_episode_numbers():
for entry in os.scandir(base_dir):
if not entry.is_file():
continue
name = entry.name
match = re.search(r'\d+', name) # first number anywhere in the name
if not match:
continue # no digits at all
number = match.group()
if len(number) != 1:
continue
padded = number.zfill(2)
new_name = re.sub(r'\d+', padded, name, count=1)
old_path = entry.path
new_path = os.path.join(base_dir, new_name)
if os.path.exists(new_path):
print(f"[WARN] Skipping rename {name} -> {new_name}, target exists")
continue
os.rename(old_path, new_path)
print(f"[OK] {name} -> {new_name}")
def put_files_into_subfolders(fileext = ".mp4"):
for file in os.listdir(base_dir):
if not file.lower().endswith(fileext):
continue
source_path = os.path.join(base_dir, file)
match = re.search(r'\d+', file)
if not match:
print(f"[WARN] Error {file}")
continue
number = match.group()
folder_name = number.zfill(2)
target_folder = os.path.join(base_dir, folder_name)
destination_path = os.path.join(target_folder, file)
#shutil.copy2(source_path, destination_path)
shutil.move(source_path, destination_path)
print("Move into Subfolders Done")
def pull_files_from_subfolders(fileext = ".mkv"):
for root, dirs, files in os.walk(base_dir):
if root == base_dir:
continue
for file in files:
if not file.lower().endswith(fileext): #comment out if all files wanted
continue #comment out for all files
source_path = os.path.join(root, file)
destination_path = os.path.join(base_dir, file)
if os.path.exists(destination_path):
name, ext = os.path.splitext(file)
counter = 1
while os.path.exists(destination_path):
destination_path = os.path.join(base_dir, f"{name}_{counter}{ext}")
counter += 1
#shutil.copy2(source_path, destination_path)
shutil.move(source_path, destination_path) #move
print("done:", base_dir)
def create_folders(amount):
for i in range(1, amount+1):
folder_name = f"{i:02d}"
folder_path = os.path.join(base_dir, folder_name)
os.makedirs(folder_path, exist_ok=True)
print("Folder Creation Done.")
# name a subtitle file according to the video file. only 2 files per folder allowed (mp3 + vtt)
def rename_files():
for entry in os.scandir(base_dir):
if not entry.is_dir():
continue
folder = entry.path
video_name = None
subtitle_name = None
for file in os.listdir(folder):
if file.lower().endswith('.mp4'):
video_name = file
if file.lower().endswith('.vtt'):
subtitle_name = file
base, _ = os.path.splitext(video_name)
new_name = base + ".vtt"
old = os.path.join(folder, subtitle_name)
new = os.path.join(folder, new_name)
os.rename(old, new)
print("renaming done")
if __name__ == "__main__":
#create_folders(12)
#fix_episode_numbers()
#put_files_into_subfolders()
#rename_files()
###MKVtoolnix
#pull_files_from_subfolders(".vtt")
print("all done")