Skip to content

Commit

Permalink
fixed sort button/filesnumrenamer()
Browse files Browse the repository at this point in the history
  • Loading branch information
vihdutta committed Mar 14, 2021
1 parent 72e3797 commit a9ad8c2
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
32 changes: 15 additions & 17 deletions asfuncs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,25 @@
plaintextfore = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#ffffff;\' >'
textback = '</span>'

def filesnumrenamer(dstname, srcfiles, dstfiles, idfilelist):
filesidrenamer(dstname, srcfiles, dstfiles)
files = os.listdir(dstname)
for i, file in enumerate(files):
def filesnumrenamer(dstname, srcfiles, dstfiles, truepath, idfilelist):
filesidrenamer(dstname, srcfiles, dstfiles, truepath)

for i, file in enumerate(os.listdir(truepath)):
name, extension = os.path.splitext(file) #error #1
name = name.split('---', 1)[0]
idfilelist.append(name + '---')
fileamount = idfilelist.count(name + '---') - 1
idfilelist.append(name + '---' + extension)
fileamount = idfilelist.count(name + '---' + extension) - 1
if fileamount > 0:
iparentheses = '(' + str(fileamount) + ')'
else:
iparentheses = ''
filerename = ''.join([name, iparentheses, extension])
os.rename(os.path.join(dstname, file), os.path.join(dstname, filerename))
os.rename(os.path.join(truepath, file), os.path.join(truepath, filerename))


def filesidrenamer(dstname, srcfiles, dstfiles):
def filesidrenamer(dstname, srcfiles, dstfiles, truepath):
for root, dirs, files in os.walk(dstname):
for file in os.listdir(dstname):
for file in os.listdir(truepath):
num = createid(srcfiles, dstfiles)
if '(' in file:
try:
Expand All @@ -38,20 +38,18 @@ def filesidrenamer(dstname, srcfiles, dstfiles):
fileider = file.split('.')[0] + num + '.' + file.split('.')[1]
except IndexError:
fileider = file + num
os.rename(os.path.join(dstname, file), os.path.join(dstname, fileider))
os.rename(os.path.join(truepath, file), os.path.join(truepath, fileider))



def dstsort(dstname):
foldername = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}'
os.mkdir(os.path.join(dstname, foldername))
files = [f for f in os.listdir(dstname) if os.path.isfile(os.path.join(dstname, f))]

def dstsort(dstname, truepath):
files = [f for f in os.listdir(truepath) if os.path.isfile(os.path.join(truepath, f))]
for file in files:
name, extension = os.path.splitext(file)
newpath = os.path.join(dstname, foldername, extension)
newpath = os.path.join(truepath, extension)
if not os.path.exists(newpath):
os.makedirs(newpath)
shutil.move(os.path.join(dstname, file), os.path.join(dstname, foldername, extension, name+extension))
shutil.move(os.path.join(truepath, file), os.path.join(truepath, extension, name+extension))


def createid(srcfiles, dstfiles):
Expand Down
48 changes: 35 additions & 13 deletions autosort.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import os
import shutil
from datetime import datetime
from asfuncs import (createid, dstsort, filesnumrenamer, filesidrenamer,
from asfuncs import (createid, dstsort, filesnumrenamer,
richtextforenl, richtextfore, plaintextfore, textback)

ignoredirs = ('$RECYCLE.BIN', 'System Volume Information')
Expand All @@ -13,9 +13,15 @@
idfilelist = []

# base of the copyfromallusbs() function
def copyfromdirs(metadata, replace, sortmethod, dstname):
def copyfromdirs(metadata, replace, fcmethod, sort, dstname):
allsrcfileslen = 0

if fcmethod == 1:
foldername = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}'
os.mkdir(os.path.join(dstname, foldername))
else:
pass

for srcdir in srcdirs:
for root, dirs, files in os.walk(srcdir):
dirs[:] = [d for d in dirs if d not in ignoredirs]
Expand All @@ -30,6 +36,17 @@ def copyfromdirs(metadata, replace, sortmethod, dstname):
dstfiles.append(file)

for srcdir in srcdirs:
if fcmethod == 2:
foldername = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}'
os.mkdir(os.path.join(dstname, foldername))
else:
pass

if 'foldername' in locals():
truepath = os.path.join(dstname, foldername)
else:
truepath = dstname

yield f'{richtextforenl}In directory {srcdir}{textback}'

for root, dirs, files in os.walk(srcdir):
Expand All @@ -48,31 +65,36 @@ def copyfromdirs(metadata, replace, sortmethod, dstname):

yield f'{plaintextfore}Copying "{file}"... {tempallsrcfileslen} of {allsrcfileslen} remain.{textback}'
tempallsrcfileslen -= 1

if metadata:
shutil.copy2(os.path.join(root, file), os.path.join(dstname, fileider))
shutil.copy2(os.path.join(root, file), os.path.join(truepath, fileider))
else:
shutil.copy(os.path.join(root, file), os.path.join(dstname, fileider))
shutil.copy(os.path.join(root, file), os.path.join(truepath, fileider))
dstfiles.append(fileider)

if sortmethod == 2:
if fcmethod == 2:
if not replace:
yield f'{richtextforenl}Enumerating...{textback}'
filesnumrenamer(dstname, srcfiles, dstfiles, idfilelist)
dstsort(dstname)
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, idfilelist)
if sort:
dstsort(dstname, truepath)
yield f'{richtextfore}Finished{textback}'
idfilelist.clear()

if sortmethod == 1:
if fcmethod == 1:
if not replace:
yield f'{richtextforenl}Enumerating...{textback}'
filesnumrenamer(dstname, srcfiles, dstfiles, idfilelist)
dstsort(dstname)
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, idfilelist)
if sort:
dstsort(dstname, truepath)
yield f'{richtextfore}Finished{textback}'

elif sortmethod == 0:
elif fcmethod == 0:
if not replace:
yield f'{richtextforenl}Enumerating...{textback}'
filesnumrenamer(dstname, srcfiles, dstfiles, idfilelist)
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, idfilelist)
if sort:
dstsort(dstname, truepath)
yield f'{richtextfore}Finished{textback}'

idfilelist.clear()
Expand Down
28 changes: 15 additions & 13 deletions autosortgui_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,18 @@
class WorkerSignals(QtCore.QObject):
result = QtCore.pyqtSignal(object)
class Worker(QtCore.QRunnable):
def __init__(self, metadata, replace, sortmethod, destdir):
def __init__(self, metadata, replace, fcmethod, sort, destdir):
super().__init__()
self.signals = WorkerSignals()
self.metadata = metadata
self.replace = replace
self.sortmethod = sortmethod
self.fcmethod = fcmethod
self.sort = sort
self.destdir = destdir

@QtCore.pyqtSlot()
def run(self):
function = copyfromdirs(self.metadata, self.replace, self.sortmethod, self.destdir)
function = copyfromdirs(self.metadata, self.replace, self.fcmethod, self.sort, self.destdir)
for statement in function:
self.signals.result.emit(statement)

Expand Down Expand Up @@ -523,15 +524,16 @@ def run_button_click(self):
self.console.clear()
organization = self.organization_box.currentText()

if organization == 'One folder organization':
sortmethod = 1
elif organization == 'Multi-folder organization':
sortmethod = 2
if organization == 'Single-folder creation':
fcmethod = 1
elif organization == 'Multi-folder creation':
fcmethod = 2
else:
sortmethod = 0
fcmethod = 0

replace = self.replace_button.isChecked()
metadata = not self.metadata_button.isChecked()
sort = self.filesort_button.isChecked()

if not srcdirs:
nosourceerror = QMessageBox()
Expand All @@ -541,7 +543,7 @@ def run_button_click(self):
nosourceerror.exec_()
try:
if os.path.isdir(self.destdir):
worker = Worker(metadata, replace, sortmethod, self.destdir)
worker = Worker(metadata, replace, fcmethod, sort, self.destdir)
worker.signals.result.connect(self.statement_returner)
self.threadpool.start(worker)
else:
Expand All @@ -565,7 +567,7 @@ def statement_returner(self, statement):
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "autosort"))
self.metadata_button.setText(_translate("MainWindow", "Disregard file metadata"))
self.metadata_button.setText(_translate("MainWindow", "Remove file metadata"))
self.destination_button.setText(_translate("MainWindow", "Destination"))
self.source_button.setText(_translate("MainWindow", "Source"))
self.console.setHtml(_translate("MainWindow", "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\">\n"
Expand All @@ -575,9 +577,9 @@ def retranslateUi(self, MainWindow):
"<p style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; -qt-user-state:131073;\"><br /></p></body></html>"))
self.sourcedisplay.setPlaceholderText(_translate("MainWindow", "Source Directories"))
self.destinationdisplay.setPlaceholderText(_translate("MainWindow", "Destination Directory"))
self.organization_box.setItemText(0, _translate("MainWindow", "No organization"))
self.organization_box.setItemText(1, _translate("MainWindow", "One folder organization"))
self.organization_box.setItemText(2, _translate("MainWindow", "Multi-folder organization"))
self.organization_box.setItemText(0, _translate("MainWindow", "No folder creation"))
self.organization_box.setItemText(1, _translate("MainWindow", "Single-folder creation"))
self.organization_box.setItemText(2, _translate("MainWindow", "Multi-folder creation"))
self.run_button.setText(_translate("MainWindow", "Run"))
self.replace_button.setText(_translate("MainWindow", "Replace duplicate files"))
self.filesort_button.setText(_translate("MainWindow", "Sort each file type"))
Expand Down

0 comments on commit a9ad8c2

Please sign in to comment.