From c40bc4be5491ee1531da843f4dcb8ec9ab84cd34 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Fri, 11 Nov 2022 15:26:35 -0800 Subject: [PATCH] Update filenamescrambleint. --- filenamescrambleint.py | 39 +++++++++++++++++++++++++++++--------- filenamescrambleordered.py | 37 +++++++++++++++++++++++++++--------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/filenamescrambleint.py b/filenamescrambleint.py index a2c72e7..da98281 100644 --- a/filenamescrambleint.py +++ b/filenamescrambleint.py @@ -1,20 +1,41 @@ ''' -Drag a file on top of this .py file, and it will have its -filename scrambled into a combination of 12 digits. +Drag multiple files on top of this .py file and their names will be scrambled +into random digits. ''' +import argparse import os import random import string import sys +from voussoirkit import betterhelp from voussoirkit import pathclass +from voussoirkit import vlogging -argv = sys.argv[1:] +log = vlogging.getLogger(__name__, 'filenamescrambleint') -for path in sorted(pathclass.glob_many(argv), key=pathclass.natural_sorter): - newname = [random.choice(string.digits) for x in range(12)] - newname = ''.join(newname) + path.dot_extension - newname = path.parent.with_child(newname) - os.rename(path, newname) - print('%s -> %s' % (path.absolute_path, newname.basename)) +def filenamescrambleint_argparse(args): + length = int(args.length) + + for path in sorted(pathclass.glob_many(args.patterns), key=pathclass.natural_sorter): + randname = [random.choice(string.digits) for x in range(length)] + randname = int(''.join(randname)) + newname = str(randname).rjust(length, '0') + path.dot_extension + newname = path.parent.with_child(newname) + os.rename(path, newname) + print('%s -> %s' % (path.absolute_path, newname.basename)) + + return 0 + +@vlogging.main_decorator +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument('patterns', nargs='+') + parser.add_argument('--length', default=12) + parser.set_defaults(func=filenamescrambleint_argparse) + + return betterhelp.go(parser, argv) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:])) diff --git a/filenamescrambleordered.py b/filenamescrambleordered.py index 11ad781..6833b76 100644 --- a/filenamescrambleordered.py +++ b/filenamescrambleordered.py @@ -4,21 +4,40 @@ name randomly scrambled into 12 digits. The others will increment that number by 1. ''' +import argparse import os import random import string import sys +from voussoirkit import betterhelp from voussoirkit import pathclass +from voussoirkit import vlogging -argv = sys.argv[1:] +log = vlogging.getLogger(__name__, 'filenamescrambleordered') -randname = [random.choice(string.digits) for x in range(12)] -randname = int(''.join(randname)) +def filenamescrambleordered_argparse(args): + length = int(args.length) + randname = [random.choice(string.digits) for x in range(length)] + randname = int(''.join(randname)) -for path in sorted(pathclass.glob_many(argv), key=pathclass.natural_sorter): - newname = str(randname).rjust(12, '0') + path.dot_extension - randname += 1 - newname = path.parent.with_child(newname) - os.rename(path, newname) - print('%s -> %s' % (path.absolute_path, newname.basename)) + for path in sorted(pathclass.glob_many(args.patterns), key=pathclass.natural_sorter): + newname = str(randname).rjust(length, '0') + path.dot_extension + randname += 1 + newname = path.parent.with_child(newname) + os.rename(path, newname) + print('%s -> %s' % (path.absolute_path, newname.basename)) + + return 0 + +@vlogging.main_decorator +def main(argv): + parser = argparse.ArgumentParser() + parser.add_argument('patterns', nargs='+') + parser.add_argument('--length', default=12) + parser.set_defaults(func=filenamescrambleordered_argparse) + + return betterhelp.go(parser, argv) + +if __name__ == '__main__': + raise SystemExit(main(sys.argv[1:]))