Created
February 19, 2024 21:18
-
-
Save shinkou/557faab7d740803b9994dda506f3a76f to your computer and use it in GitHub Desktop.
Randomized File Renamer
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
#!/usr/bin/env python3 | |
# vim: fileencoding=utf-8 ff=unix | |
from math import log | |
from random import randint | |
import argparse | |
import logging | |
import os | |
import re | |
import sys | |
logger = logging.getLogger(__name__) | |
logger.setLevel(logging.DEBUG) | |
handler = logging.StreamHandler() | |
handler.setLevel(logging.DEBUG) | |
formatter = logging.Formatter('%(message)s') | |
handler.setFormatter(formatter) | |
logger.addHandler(handler) | |
def rndstr(size, ucase=True, lcase=True, num=True, uscr=True, sybl=False): | |
'''Generate random string''' | |
sot = '' | |
if ucase: | |
sot += 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
if lcase: | |
sot += 'abcdefghijklmnopqrstuvwxyz' | |
if num: | |
sot += '0123456789' | |
if uscr: | |
sot += '_' | |
if sybl: | |
sot += r'`-=[]\;' + "'" + ',./~!@#$%^&*()+{}|:"<>?' | |
sotlen = len(sot) | |
return ''.join([sot[randint(0, sotlen - 1)] for i in range(0, size)]) | |
def swap_basename(src, bn): | |
'''Swap basename''' | |
d = re.findall(r'^.*/', src) | |
ext = re.findall(r'\..*$', src) | |
if 0 < len(d) and 0 < len(ext): | |
logger.info(f'"{src}" -> "{d[0]}{bn}{ext[0]}"') | |
os.rename(src, f"{d[0]}{bn}{ext[0]}") | |
elif 0 < len(d): | |
logger.info(f'"{src}" -> "{d[0]}{bn}"') | |
os.rename(src, f"{d[0]}{bn}") | |
elif 0 < len(ext): | |
logger.info(f'"{src}" -> "{bn}{ext[0]}"') | |
os.rename(src, f"{bn}{ext[0]}") | |
def getargs(): | |
'''Get and process command line arguments''' | |
parser = argparse.ArgumentParser(description='Randomized File Renamer') | |
prefix_grp = parser.add_mutually_exclusive_group() | |
prefix_grp.add_argument( | |
'--prefix' | |
, type=str | |
, help='filename prefix (cannot be used with --prefix-size)' | |
) | |
prefix_grp.add_argument( | |
'--prefix-size' | |
, type=int | |
, default=4 | |
, help='random filename prefix size (cannot be used with --prefix' | |
', default:4)' | |
) | |
parser.add_argument( | |
'--symbols' | |
, action='store_true' | |
, help='to include symbolic characters' | |
) | |
parser.add_argument( | |
'--no-upper' | |
, action='store_true' | |
, help='to exclude uppercase characters' | |
) | |
parser.add_argument( | |
'--no-lower' | |
, action='store_true' | |
, help='to exclude lowercase characters' | |
) | |
parser.add_argument( | |
'--no-num' | |
, action='store_true' | |
, help='to exclude numeric characters' | |
) | |
parser.add_argument( | |
'--no-underscore' | |
, action='store_true' | |
, help='to exclude underscore character' | |
) | |
parser.add_argument('fpaths', nargs='+') | |
return parser.parse_args() | |
if '__main__' == __name__: | |
args = getargs() | |
prefix = args.prefix if args.prefix else rndstr( | |
args.prefix_size | |
, not args.no_upper | |
, not args.no_lower | |
, not args.no_num | |
, not args.no_underscore | |
, args.symbols | |
) | |
suffix_width = "0%d" % int(log(len(args.fpaths), 10) + 1) | |
m = {} | |
idx = 0 | |
for abspath in [os.path.abspath(fpath) for fpath in args.fpaths]: | |
m[abspath] = f"{prefix}_{idx:{suffix_width}}" | |
idx += 1 | |
for k, v in m.items(): | |
swap_basename(k, v) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment