forked from avinashkranjan/Amazing-Python-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfmover.py
285 lines (244 loc) · 10.2 KB
/
fmover.py
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import os
import shutil
import PySimpleGUI as sg
source_folder = sg.popup_get_folder('Choose source location:', default_path='')
destination_folder = sg.popup_get_folder('Choose destination folder:',
default_path='')
file_type = []
mode_list = []
sort_list = []
image_list = ['.png', '.jpg', '.jpeg', '.gif']
archive_list = ['.zip', '.rar', '.7z']
textf_list = ['.txt', '.md', '.pdf', '.doc', '.docx']
def get_path(src_or_dst):
folders = {source_folder: destination_folder}
source = folders.keys()
destination = folders.values()
if src_or_dst == 'src':
return str(*source)
elif src_or_dst == 'dst':
return str(*destination)
else:
raise SystemError("Parameter has to be 'src' or 'dst'")
class fmGUI:
def main_window(self):
layout = [
[sg.Text("Choose Operation to perform:")],
[
sg.Combo(['Copy', 'Move'],
default_value='Move',
key='OPERATION')
],
[
sg.Frame(layout=[[sg.Text("Sort by Type")],
[
sg.Radio("Enabled",
"RADIO1",
default=False,
key='SBYTE',
enable_events=True),
sg.Radio("Disabled",
"RADIO1",
default=True,
key='SBYTD',
enable_events=True)
]],
title='Sorting Options',
title_color='red',
relief=sg.RELIEF_SUNKEN)
], [sg.Text("Choose filetype:")],
[
sg.Combo([
"Archive ('.zip', '.rar'...)", "Image ('.png', '.jpg'...)",
"Text ('.txt', '.docx'...)"
],
key='FILETYPE',
enable_events=True)
], [sg.Ok(), sg.Cancel()]
]
window = sg.Window('Choose filetype to move',
layout,
default_element_size=(40, 1))
while True:
event, values = window.read()
if event in (sg.WIN_CLOSED, 'Cancel'):
break
elif event in 'Ok':
if values['FILETYPE'] not in file_type:
append_file_type(values['FILETYPE'])
run_fmover = FileMover()
append_mode(values['OPERATION'])
if len(sort_list) == 1:
for value in sort_list:
if value == 'Sort by Type':
run_fmover.filemover(values['OPERATION'],
value)
else:
run_fmover.filemover(values['OPERATION'], None)
else:
run_fmover.filemover(values['OPERATION'], None)
else:
run_fmover = FileMover()
append_mode(values['OPERATION'])
if len(sort_list) == 1:
for value in sort_list:
if value == 'Sort by Type':
run_fmover.filemover(values['OPERATION'],
value)
else:
run_fmover.filemover(values['OPERATION'], None)
else:
run_fmover.filemover(values['OPERATION'], None)
elif event in 'SBYTE':
if values['SBYTE'] is True:
sort_list.append('Sort by Type')
else:
pass
elif event in 'SBYTD':
if values['SBYTD'] is True:
sort_list.clear()
else:
pass
else:
pass
window.close()
def translate_filetype():
for value in file_type:
if value.startswith("Archive"):
file_type.clear()
for arch in archive_list:
file_type.append(arch)
return str(file_type)
elif value.startswith("Image"):
file_type.clear()
for img in image_list:
file_type.append(img)
return str(file_type)
elif value.startswith("Text"):
file_type.clear()
for txt in textf_list:
file_type.append(txt)
return str(file_type)
else:
pass
def append_mode(mode):
mode_list.append(mode)
if mode in mode_list:
return mode
def detect_mode():
return mode_list[0]
def append_file_type(value):
if len(file_type) == 1:
file_type.clear()
else:
pass
file_type.append(value)
translate_filetype()
return value
class FileMover():
def filemover(self, operation, sortby):
while True:
num_files = len(os.listdir(get_path('src')))
if num_files == 0:
sg.PopupError("No files in folder!")
raise SystemExit()
elif sortby is None:
for file in os.listdir(get_path('src')):
#file_ending = get_file_type()
is_file_in_curr_dir = os.path.isfile(
get_path('dst') + "/" + file)
for value in file_type:
if file.endswith(value):
result = None
if is_file_in_curr_dir is False:
if operation == "Copy":
result = shutil.copy(
get_path('src') + "/" + file,
get_path('dst') + "/" + file)
else:
result = shutil.move(
get_path('src') + "/" + file,
get_path('dst') + "/" + file)
# if file not in current_dir:
# file_type.pop()
elif sortby == 'Sort by Type':
for file in os.listdir(get_path('src')):
sc = SortCriteria()
is_file_in_dst_dir = os.path.isfile(
get_path('dst') + "/" + file)
get_subdir()
for value in file_type:
if file.endswith(value):
if is_file_in_dst_dir is False and get_subdir(
) is False:
result = None
if operation == "Copy":
result = shutil.copy(
get_path('src') + "/" + file,
sc.sortbytype(value) + "/" + file)
else:
result = shutil.move(
get_path('src') + "/" + file,
sc.sortbytype(value) + "/" + file)
elif is_file_in_dst_dir is False and get_subdir(
) is True:
result = None
if operation == 'Copy':
result = shutil.copy(
get_path('src') + "/" + file,
sc.sortbytype(value) + "/" + file)
else:
result = shutil.move(
get_path('src') + "/" + file,
sc.sortbytype(value) + "/" + file)
else:
pass
return sg.PopupOK(
f"File transfer successful!\nFile(s) moved to '{get_path('dst')}'"
)
fmover = FileMover()
source = get_path('src')
destination = get_path('dst')
def get_subdir():
if os.path.exists(str(destination) + '/' + 'Images'):
return True
elif os.path.exists(str(destination) + '/' + 'Archives'):
return True
elif os.path.exists(str(destination) + '/' + 'Text Files'):
return True
else:
return False
class SortCriteria():
def sortbytype(self, ftype):
type_list = [ftype]
# For image files
for type in type_list:
if type in image_list: # '.png' or '.jpg' or '.jpeg' or '.gif':
if os.path.exists(str(destination) + '/' + 'Images'):
return str(os.path.join(str(destination) + '/' + 'Images'))
else:
os.mkdir(str(destination) + '/' + 'Images')
return str(os.path.join(str(destination) + '/' + 'Images'))
# For archive files
elif type in archive_list:
if os.path.exists(str(destination) + '/' + 'Archives'):
return str(
os.path.join(str(destination) + '/' + 'Archives'))
else:
os.mkdir(str(destination) + '/' + 'Archives')
return str(
os.path.join(str(destination) + '/' + 'Archives'))
# For text files
elif type in textf_list:
if os.path.exists(str(destination) + '/' + 'Text Files'):
return str(
os.path.join(str(destination) + '/' + 'Text Files'))
else:
os.mkdir(str(destination) + '/' + 'Text Files')
return str(
os.path.join(str(destination) + '/' + 'Text Files'))
else:
sg.PopupError("File type not found!")
raise SystemExit()
main_gui = fmGUI()
main_gui.main_window()