Update filenamescrambleint.

This commit is contained in:
voussoir 2022-11-11 15:26:35 -08:00
parent 9ed28ac6b0
commit c40bc4be54
No known key found for this signature in database
GPG key ID: 5F7554F8C26DACCB
2 changed files with 58 additions and 18 deletions

View file

@ -1,20 +1,41 @@
''' '''
Drag a file on top of this .py file, and it will have its Drag multiple files on top of this .py file and their names will be scrambled
filename scrambled into a combination of 12 digits. into random digits.
''' '''
import argparse
import os import os
import random import random
import string import string
import sys import sys
from voussoirkit import betterhelp
from voussoirkit import pathclass 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): def filenamescrambleint_argparse(args):
newname = [random.choice(string.digits) for x in range(12)] length = int(args.length)
newname = ''.join(newname) + path.dot_extension
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) newname = path.parent.with_child(newname)
os.rename(path, newname) os.rename(path, newname)
print('%s -> %s' % (path.absolute_path, newname.basename)) 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:]))

View file

@ -4,21 +4,40 @@ name randomly scrambled into 12 digits. The others will increment that number
by 1. by 1.
''' '''
import argparse
import os import os
import random import random
import string import string
import sys import sys
from voussoirkit import betterhelp
from voussoirkit import pathclass 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)] def filenamescrambleordered_argparse(args):
length = int(args.length)
randname = [random.choice(string.digits) for x in range(length)]
randname = int(''.join(randname)) randname = int(''.join(randname))
for path in sorted(pathclass.glob_many(argv), key=pathclass.natural_sorter): for path in sorted(pathclass.glob_many(args.patterns), key=pathclass.natural_sorter):
newname = str(randname).rjust(12, '0') + path.dot_extension newname = str(randname).rjust(length, '0') + path.dot_extension
randname += 1 randname += 1
newname = path.parent.with_child(newname) newname = path.parent.with_child(newname)
os.rename(path, newname) os.rename(path, newname)
print('%s -> %s' % (path.absolute_path, newname.basename)) 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:]))