-
Notifications
You must be signed in to change notification settings - Fork 0
/
batchful.py
378 lines (280 loc) · 10.5 KB
/
batchful.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
# region modules
import webbrowser
import os
import shutil
# Tries to import kivy
try:
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.properties import ObjectProperty
from kivy.lang import Builder
from kivy.uix.popup import Popup
from kivy.uix.label import Label
usingKivy = True
usingTkinter = False
except:
# tkinter modules
try:
# For python 3+
from tkinter import *
from tkinter.ttk import *
from tkinter import messagebox
except:
# For python 2+
from Tkinter import *
from ttk import *
import tkMessageBox as messagebox
usingTkinter = True
usingKivy = False
# endregion
# region variables
filePath = os.path.abspath(__file__) # Includes 'batchful.py'
currentLocation = os.path.dirname(os.path.abspath(__file__)) # Doesn't include 'batchful.py'
xSize = "500"
ySize = "500"
if usingKivy:
usingTkinter = False
elif usingTkinter:
usingKivy = False
else:
print("ERROR: MODULES NOT IMPORTED CORRECTLY")
# endregion
# region kivy setup
if usingKivy:
class MainWindow(Screen):
"""Main batchful window"""
dir = ObjectProperty(None)
def sort(self):
if self.dir.text != "":
change_loc(self.dir.text)
def git_hub(self):
open_git_hub()
class SortingWindow(Screen):
pass
class ExtWindow(Screen):
"""Sort by extension window"""
sort_sub_folders = False
def by_ext_button(self):
by_ext(self.sort_sub_folders)
def check_active_ext(self, *args):
if args[1]:
self.sort_sub_folders = True
else:
self.sort_sub_folders = False
class NameWindow(Screen):
"""Sort by name window"""
sort_sub_folders = False
phrase = ObjectProperty(None)
def by_name_button(self):
by_name(self.phrase.text, self.sort_sub_folders)
def check_active_name(self, *args):
if args[1]:
self.sort_sub_folders = True
else:
self.sort_sub_folders = False
class HelpWindow(Screen):
"""Help window"""
pass
class WindowManager(ScreenManager):
pass
def show_moved_files(num):
"""Creates popup with the number of moved files"""
pop = Popup(title="batchful",
content=Label(text="Moved " + str(num) + " File(s)"),
size_hint=(None, None), size=(200, 200))
pop.open()
kv = Builder.load_file("batchful.kv")
class batchfulApp(App):
def build(self):
return kv
# endregion
# region functions
# region sub-folders
def combine_funcs(*funcs):
"""Combines several functions into one"""
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def sub_folders(name, extension):
"""Searches sub-folders for specific files"""
for root, subdirs, files in os.walk(currentLocation):
files = [f for f in files if not f[0] == '.']
subdirs[:] = [d for d in subdirs if not d[0] == '.']
for file in files:
path = os.path.join(root, file)
file_name = os.path.splitext(path)[0]
if name in file_name:
if root != currentLocation and path != filePath:
print(file)
shutil.move(path, currentLocation)
check_if_empty()
def empty_sub_folders():
"""Empties all sub-folders"""
for root, subdirs, files in os.walk(currentLocation):
files = [f for f in files if not f[0] == '.']
subdirs[:] = [d for d in subdirs if not d[0] == '.']
check_if_empty()
for file in files:
print(root, currentLocation)
if root != currentLocation:
path = os.path.join(root, file)
shutil.move(path, currentLocation)
check_if_empty()
def check_if_empty():
"""Deletes empty sub-folders"""
for root, subdirs, files in os.walk(currentLocation):
files = [f for f in files if not f[0] == '.']
subdirs[:] = [d for d in subdirs if not d[0] == '.']
if not files:
os.rmdir(root)
# endregion
# region sorters
def by_ext(sort_sub_folders):
"""Sorts files by extension"""
if usingTkinter:
if sortSubFolders:
sort_sub_folders = True
if sort_sub_folders:
empty_sub_folders()
for root, subdirs, files in os.walk(currentLocation):
files = [f for f in files if not f[0] == '.']
subdirs[:] = []
count = 0
for file in files:
path = os.path.join(root, file)
file_extension = os.path.splitext(path)[1]
if path != filePath and file != "batchful.kv":
if file_extension != "":
folder_path = os.path.join(root, file_extension)
else:
folder_path = os.path.join(root, "FILE")
if not os.path.isdir(file_extension):
os.mkdir(folder_path)
shutil.move(path, folder_path)
count += 1
if usingTkinter:
messagebox.showinfo(title="batchful", message="Moved " + str(count) + " File(s)")
elif usingKivy:
show_moved_files(count)
def sort_by_name():
"""Create input window for tkinter"""
def get_name():
by_name(ask_name.get())
ask_window = Tk()
ask_window.title("batchful")
ask_window.geometry("200x200")
title = Label(ask_window, text="Enter A Phrase")
title.grid(column=1, row=0)
ask_name = Entry(ask_window, width=10)
ask_name.focus()
ask_name.grid(column=1, row=1)
ask_button = Button(ask_window, text="Search", command=combine_funcs(get_name, ask_window.destroy))
ask_button.grid(column=1, row=2)
quit_button = Button(ask_window, text="Go Back", command=ask_window.destroy)
quit_button.grid(column=0, row=3)
def by_name(name, sort_sub_folders):
"""Sorts files by specific input"""
if usingTkinter:
if sortSubFolders.get() == 1:
sort_sub_folders = True
if sort_sub_folders:
sub_folders(name, None)
for root, subdirs, files in os.walk(currentLocation):
files = [f for f in files if not f[0] == '.']
subdirs[:] = []
count = 0
for file in files:
path = os.path.join(root, file)
file_name = os.path.splitext(path)[0]
if path != filePath and file != "batchful.kv":
folder_path = os.path.join(root, name)
if not os.path.isdir(name):
if not os.path.isfile(name):
os.mkdir(folder_path)
else:
temp_folder_path = os.path.join(root, "TEMP FOLDER")
if not os.path.isdir(temp_folder_path) and not os.path.isfile(temp_folder_path):
os.mkdir(temp_folder_path)
shutil.move(path, temp_folder_path)
print("YAY")
path = os.path.join(path, temp_folder_path)
if not os.path.isfile(name):
os.mkdir(folder_path)
shutil.move(path, folder_path)
count += 1
if name in file_name:
shutil.move(path, folder_path)
count += 1
if usingTkinter:
messagebox.showinfo(title="batchful", message="Moved " + str(count) + " File(s)")
elif usingKivy:
show_moved_files(count)
# endregion
# region other
def git_hub():
"""Creates GitHub popup window for tkinter"""
open = messagebox.askquestion(title="GitHub", message="Open GitHub?")
if open == "yes":
open_git_hub()
def open_git_hub():
"""Opens GitHub"""
webbrowser.open("https://github.com/batchful/batchful-python")
def help():
"""Creates help window for tkinter"""
help_window = Tk()
help_window.title("batchful - help")
help_text = Label(help_window, text="HOW TO USE BATCHFUL:", font=("Ariel Bold", 25))
help_text2 = Label(help_window, text="In the main screen, choose a method of organization.\n\
Currently, there are 2 methods of organization: \n\
1. By file extension: creates a directory for every filetype \n\
and sorts the files accordingly. \n\
2. By file name: creates a directory for a spacefic phrase and moves \n\
all of the files containing the phrase to the directory. \n\
\n\
You will also be presented with the option to sort through subfolders. \n\
This process deletes the unnecessary folders after it's done sorting. \n\
\n\
To run the program, just press the desired button. \n\
you can make sub-folders search active by checking the checkbox", font=("Ariel", 15))
back_button = Button(help_window, text="Go Back", command=help_window.destroy)
current_location_label = Label(help_window, text="Current location: " + str(currentLocation), font=("Ariel 12"))
help_text.grid(column=0, row=0)
help_text2.grid(column=0, row=1)
current_location_label.grid(column=0, row=3)
back_button.grid(column=0, row=4)
place_holder = Label(help_window).grid(column=0, row=2)
def change_loc(new_loc):
global currentLocation
currentLocation = new_loc
# endregion
# endregion
# region main loop
if usingKivy:
batchfulApp().run()
elif usingTkinter:
window = Tk()
window.title("batchful")
window.geometry(xSize + "x" + ySize)
# window.wm_iconbitmap("Logo.ico")
# window.tk_setPalette("black")
title = Label(window, text="batchful", font=("Ariel Bold", 50))
title.grid(column=1, row=0)
extensionButton = Button(window, text="Sort By Extension", command=by_ext)
extensionButton.grid(column=0, row=1)
nameButton = Button(window, text="Sort By Name", command=sort_by_name)
nameButton.grid(column=1, row=1)
sortSubFolders = IntVar()
sortSubFoldersCheckBox = Checkbutton(window, text="Search Sub-Folders", var=sortSubFolders)
sortSubFoldersCheckBox.grid(column=2, row=1)
helpButton = Button(window, text="Help", command=help)
helpButton.grid(column=0, row=3)
gitHubButton = Button(window, text="GitHub", command=git_hub)
gitHubButton.grid(column=1, row=3)
quitButton = Button(text="Quit", command=window.destroy)
quitButton.grid(column=2, row=5)
placeholder = Label(window).grid(column=0, row=2)
placeholder2 = Label(window).grid(column=0, row=4)
window.mainloop()
# endregion