-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
109 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,53 @@ | ||
import os | ||
import shutil | ||
from datetime import datetime | ||
from pathlib import Path, PurePath | ||
from random import randint | ||
from shutil import move | ||
|
||
RICH_TEXT_FORE_NL = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#a89769;\' ><br>' | ||
RICH_TEXT_FORE = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#a89769;\' >' | ||
PLAIN_TEXT_FORE = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#ffffff;\' >' | ||
# styling | ||
RICH_TEXTFORE_NL = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#a89769;\' ><br>' | ||
RICH_TEXTFORE = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#a89769;\' >' | ||
PLAIN_TEXTFORE = '<span style=\' font-family:"Segoe UI Light"; font-size:18pt; font-weight:400; color:#ffffff;\' >' | ||
TEXTBACK = '</span>' | ||
|
||
def filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist): | ||
filesidrenamer(dstname, srcfiles, dstfiles, truepath) | ||
|
||
for file in Path(truepath).iterdir(): | ||
name, extension = PurePath(file).stem, PurePath(file).suffix #error #1 | ||
name = name.split('---', 1)[0] | ||
id_filelist.append(name + '---' + extension) | ||
fileamount = id_filelist.count(name + '---' + extension) - 1 | ||
if fileamount > 0: | ||
iparentheses = '(' + str(fileamount) + ')' | ||
# add number file for the directory | ||
def duplicate_tagger(file, filecount_id, dstdir, folder, name, extension): | ||
dst_filepath = Path(dstdir) / folder / file | ||
if dst_filepath.is_file(): | ||
filecount_id += 1 | ||
return duplicate_tagger(f"{name}({filecount_id}){extension}", filecount_id, dstdir, folder, name, extension) | ||
else: | ||
if filecount_id == 0: | ||
return f"{name}{extension}" | ||
else: | ||
iparentheses = '' | ||
filerename = ''.join([name, iparentheses, extension]) | ||
os.rename(os.path.join(truepath, file), os.path.join(truepath, filerename)) | ||
|
||
|
||
def filesidrenamer(dstname, srcfiles, dstfiles, truepath): | ||
for root, dirs, files in os.walk(dstname): | ||
for file in os.listdir(truepath): | ||
num = createid(srcfiles, dstfiles) | ||
if '(' in file: | ||
try: | ||
file_ider = file.split('(')[0] + num + file.split('(')[1] | ||
except IndexError: | ||
file_ider = file + num | ||
else: | ||
try: | ||
file_ider = file.split('.')[0] + num + '.' + file.split('.')[1] | ||
except IndexError: | ||
file_ider = file + num | ||
os.rename(os.path.join(truepath, file), os.path.join(truepath, file_ider)) | ||
|
||
|
||
|
||
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 = PurePath(file).stem, PurePath(file).suffix | ||
new_dir = PurePath(truepath, extension).joinpath() | ||
if not Path(new_dir).exists(): | ||
Path(new_dir).mkdir() | ||
filename = name + extension | ||
old_path = Path(truepath) / file | ||
new_path = Path(truepath) / extension / filename | ||
shutil.move(old_path, new_path) | ||
|
||
|
||
def createid(srcfiles, dstfiles): | ||
i1 = len(srcfiles) + len(dstfiles) | ||
i2 = int(str(i1) + '0') | ||
return '---' + str(randint(i1, i2)) | ||
return f"{name}({filecount_id}){extension}" | ||
|
||
|
||
# gets filecount for src and destination directories | ||
def total_filecount(srcdirs): | ||
filecount = 0 | ||
for srcdir in srcdirs: | ||
for path in Path(srcdir).rglob("*"): | ||
if path.is_file(): | ||
filecount += 1 | ||
return filecount, filecount | ||
|
||
|
||
# sorts files in directory | ||
def sortdirs(dstdir, folder): | ||
path = Path(dstdir) / folder | ||
for filepath in path.iterdir(): | ||
if filepath.is_file(): | ||
name, extension = PurePath(filepath).stem, PurePath(filepath).suffix | ||
new_dir = Path(dstdir) / folder / extension | ||
if not Path(new_dir).exists(): | ||
Path(new_dir).mkdir() | ||
filename = name + extension | ||
old_path = Path(dstdir) / folder / filepath | ||
new_path = Path(dstdir) / folder / extension / filename | ||
move(old_path, new_path) | ||
|
||
|
||
# creates folder | ||
def folder_create(dstdir): | ||
folder_name = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}' | ||
Path(PurePath(dstdir, folder_name).joinpath()).mkdir() | ||
return folder_name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,45 @@ | ||
import os | ||
import shutil | ||
from pathlib import Path, PurePath | ||
from datetime import datetime | ||
from asfuncs import (createid, dstsort, filesnumrenamer, | ||
RICH_TEXT_FORE_NL, RICH_TEXT_FORE, PLAIN_TEXT_FORE, TEXTBACK) | ||
|
||
IGNORE_DIRS = ('$RECYCLE.BIN', 'System Volume Information') | ||
|
||
metadata = False | ||
srcdirs = [] | ||
srcfiles = [] | ||
dstfiles = [] | ||
id_filelist = [] | ||
|
||
# base of the copyfromallusbs() function | ||
def copyfromdirs(metadata, replace, fcmethod, sort, dstname): | ||
all_srcfiles_len = 0 | ||
|
||
if fcmethod == 'Single-folder creation': | ||
folder_name = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}' | ||
Path(PurePath(dstname, folder_name).joinpath()).mkdir() | ||
|
||
for srcdir in srcdirs: | ||
for root, dirs, files in os.walk(srcdir): | ||
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS] | ||
for file in files: | ||
all_srcfiles_len += 1 | ||
|
||
yield f'{RICH_TEXT_FORE}Discovered {all_srcfiles_len} files in directory(s): {", ".join(srcdirs)}{TEXTBACK}' | ||
temp_all_srcfiles_len = all_srcfiles_len | ||
|
||
for root, dirs, files in os.walk(dstname): | ||
for file in files: | ||
dstfiles.append(file) | ||
|
||
for srcdir in srcdirs: | ||
if fcmethod == 'Multi-folder creation': | ||
folder_name = f'autosort {datetime.now().strftime("%H-%M-%S-%f")}' | ||
Path(PurePath(dstname, folder_name).joinpath()).mkdir() | ||
|
||
if 'folder_name' in locals(): | ||
truepath = PurePath(dstname, folder_name).joinpath() | ||
else: | ||
truepath = dstname | ||
|
||
yield f'{RICH_TEXT_FORE_NL}In directory {srcdir}{TEXTBACK}' | ||
|
||
for root, dirs, files in os.walk(srcdir): | ||
dirs[:] = [d for d in dirs if d not in IGNORE_DIRS] | ||
for file in files: | ||
srcfiles.append(file) | ||
|
||
for file in files: | ||
if not replace: | ||
try: | ||
file_ider = file.split('.')[0] + createid(srcfiles, dstfiles) + '.' + file.split('.')[1] | ||
except IndexError: | ||
file_ider = file + createid(srcfiles, dstfiles) | ||
from os import walk | ||
from shutil import copy, copy2 | ||
from pathlib import Path | ||
from asfuncs import (total_filecount, duplicate_tagger, folder_create, | ||
sortdirs, RICH_TEXTFORE_NL, RICH_TEXTFORE, PLAIN_TEXTFORE, TEXTBACK) | ||
from time import perf_counter | ||
|
||
# copies from srcdirs to dstdir + creates filenumber for files | ||
def copy_from_dirs(srcdirs, dstdir, fcmethod, sort, metadata, replace): | ||
start_time = perf_counter() | ||
folder = "" | ||
filecount, filecount_temp = total_filecount(srcdirs) | ||
yield f'{RICH_TEXTFORE}Discovered {filecount} files in directory(s): {", ".join(srcdirs)}{TEXTBACK}' | ||
|
||
if fcmethod == "Single-folder creation": | ||
folder = folder_create(dstdir) | ||
for srcdir in srcdirs: # for each source directory | ||
yield f'{RICH_TEXTFORE_NL}In directory {srcdir}{TEXTBACK}' | ||
if fcmethod == "Multi-folder creation": | ||
folder = folder_create(dstdir) | ||
for root, _, files in walk(srcdir): # get data for source directory | ||
for file in files: # iterate through each file | ||
yield f'{PLAIN_TEXTFORE}Copying "{file}"... {filecount} of {filecount_temp} remain.{TEXTBACK}' | ||
filecount_temp -= 1 | ||
filecount_id = 0 | ||
src_filepath = Path(root) / file | ||
name, extension = src_filepath.stem, src_filepath.suffix | ||
|
||
if replace: | ||
new_filename = file | ||
else: | ||
file_ider = file | ||
new_filename = duplicate_tagger(file, filecount_id, dstdir, folder, name, extension) | ||
old_path = Path(root) / file | ||
new_path = Path(dstdir) / folder / new_filename | ||
|
||
yield f'{PLAIN_TEXT_FORE}Copying "{file}"... {temp_all_srcfiles_len} of {all_srcfiles_len} remain.{TEXTBACK}' | ||
temp_all_srcfiles_len -= 1 | ||
|
||
if metadata: | ||
shutil.copy2(PurePath(root, file).joinpath(), PurePath(truepath, file_ider).joinpath()) | ||
copy2(old_path, new_path) | ||
else: | ||
shutil.copy(PurePath(root, file).joinpath(), PurePath(truepath, file_ider).joinpath()) | ||
dstfiles.append(file_ider) | ||
|
||
if fcmethod == 'Multi-folder creation': | ||
if not replace: | ||
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}' | ||
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist) | ||
if sort: | ||
dstsort(dstname, truepath) | ||
yield f'{RICH_TEXT_FORE}Finished{TEXTBACK}' | ||
id_filelist.clear() | ||
|
||
if fcmethod == 'Single-folder creation': | ||
if not replace: | ||
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}' | ||
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist) | ||
copy(old_path, new_path) | ||
if sort: | ||
dstsort(dstname, truepath) | ||
yield f'{RICH_TEXT_FORE}Finished{TEXTBACK}' | ||
|
||
elif fcmethod == 'No folder creation': | ||
if not replace: | ||
yield f'{RICH_TEXT_FORE_NL}Enumerating...{TEXTBACK}' | ||
filesnumrenamer(dstname, srcfiles, dstfiles, truepath, id_filelist) | ||
if sort: | ||
dstsort(dstname, truepath) | ||
yield f'{RICH_TEXT_FORE}Finished{TEXTBACK}' | ||
|
||
id_filelist.clear() | ||
yield f'{RICH_TEXTFORE}Sorting files...{TEXTBACK}' | ||
sortdirs(dstdir, folder) | ||
|
||
|
||
'''testing items''' | ||
# tic = time.perf_counter() | ||
# toc = time.perf_counter() | ||
# with open('testing/filenamehere.txt', 'a') as f: | ||
# f.write(f'File copied in {toc - tic:0.4f} seconds\n') | ||
end_time = perf_counter() | ||
yield f'{RICH_TEXTFORE}Finished in {start_time-end_time} seconds.{TEXTBACK}' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters