Improve fileprefix
This commit is contained in:
parent
66b69ff7f8
commit
a63b397232
1 changed files with 82 additions and 40 deletions
|
@ -5,59 +5,101 @@ of the files in the current working directory will be renamed in the format
|
||||||
integer that counts each file in the folder.
|
integer that counts each file in the folder.
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
import argparse
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import string
|
import string
|
||||||
import re
|
import re
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
assert len(sys.argv) in range(2, 4)
|
from voussoirkit import pathclass
|
||||||
|
from voussoirkit import safeprint
|
||||||
|
|
||||||
ctime = '-c' in sys.argv
|
IGNORE_EXTENSIONS = ['py', 'lnk', 'ini']
|
||||||
dry = '--dry' in sys.argv
|
|
||||||
|
|
||||||
IGNORE_EXTENSIONS = ['.py', '.lnk']
|
|
||||||
|
|
||||||
prefix = sys.argv[1]
|
def natural_sorter(x):
|
||||||
if prefix != '':
|
|
||||||
prefix += '_'
|
|
||||||
files = [os.path.abspath(x) for x in os.listdir()]
|
|
||||||
files = [item for item in files if os.path.isfile(item)]
|
|
||||||
if __file__ in files:
|
|
||||||
files.remove(__file__)
|
|
||||||
|
|
||||||
# trust me on this.
|
|
||||||
zeropadding = len(str(len(files)))
|
|
||||||
zeropadding = max(2, zeropadding)
|
|
||||||
zeropadding = str(zeropadding)
|
|
||||||
|
|
||||||
format = '%s%0{pad}d%s'.format(pad=zeropadding)
|
|
||||||
|
|
||||||
def natural_sort(l):
|
|
||||||
'''
|
'''
|
||||||
http://stackoverflow.com/a/11150413
|
http://stackoverflow.com/a/11150413
|
||||||
'''
|
'''
|
||||||
convert = lambda text: int(text) if text.isdigit() else text.lower()
|
convert = lambda text: int(text) if text.isdigit() else text.lower()
|
||||||
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
|
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
|
||||||
return sorted(l, key=alphanum_key)
|
return alphanum_key(x)
|
||||||
|
|
||||||
|
def fileprefix(
|
||||||
|
prefix='',
|
||||||
|
sep=' ',
|
||||||
|
ctime=False,
|
||||||
|
dry=False,
|
||||||
|
):
|
||||||
|
current_directory = pathclass.Path('.')
|
||||||
|
|
||||||
|
prefix = prefix.strip()
|
||||||
|
if prefix == ':':
|
||||||
|
prefix = current_directory.basename + ' - '
|
||||||
|
elif prefix != '':
|
||||||
|
prefix += sep
|
||||||
|
|
||||||
|
filepaths = current_directory.listdir()
|
||||||
|
filepaths = [f for f in filepaths if f.is_file]
|
||||||
|
filepaths = [f for f in filepaths if f.extension.lower() not in IGNORE_EXTENSIONS]
|
||||||
|
|
||||||
|
try:
|
||||||
|
pyfile = pathclass.Path(__file__)
|
||||||
|
filepaths.remove(pyfile)
|
||||||
|
except ValueError:
|
||||||
|
pass
|
||||||
|
|
||||||
|
# trust me on this.
|
||||||
|
zeropadding = len(str(len(filepaths)))
|
||||||
|
zeropadding = max(2, zeropadding)
|
||||||
|
zeropadding = str(zeropadding)
|
||||||
|
|
||||||
|
format = '{{prefix}}{{index:0{pad}d}}{{extension}}'.format(pad=zeropadding)
|
||||||
|
|
||||||
if ctime:
|
if ctime:
|
||||||
print('Sorting by time')
|
print('Sorting by time')
|
||||||
files.sort(key=os.path.getctime)
|
filepaths.sort(key=lambda x: x.stat.st_ctime)
|
||||||
else:
|
else:
|
||||||
print('Sorting by name')
|
print('Sorting by name')
|
||||||
files = natural_sort(files)
|
filepaths.sort(key=lambda x: natural_sorter(x.basename))
|
||||||
for (fileindex, filename) in enumerate(files):
|
|
||||||
if '.' in filename:
|
for (index, filepath) in enumerate(filepaths):
|
||||||
extension = '.' + filename.split('.')[-1]
|
extension = filepath.extension
|
||||||
if extension in IGNORE_EXTENSIONS:
|
if extension != '':
|
||||||
continue
|
extension = '.' + extension
|
||||||
else:
|
|
||||||
extension = ''
|
newname = format.format(prefix=prefix, index=index, extension=extension)
|
||||||
newname = format % (prefix, fileindex, extension)
|
if filepath.basename != newname:
|
||||||
if os.path.basename(filename) != newname:
|
message = filepath.basename + ' -> ' + newname
|
||||||
print(''.join([c for c in (filename + ' -> ' + newname) if c in string.printable]))
|
safeprint.safeprint(message)
|
||||||
if not dry:
|
if not dry:
|
||||||
os.rename(filename, newname)
|
os.rename(filepath.absolute_path, newname)
|
||||||
|
pass
|
||||||
if dry:
|
if dry:
|
||||||
print('Dry. No files renamed.')
|
print('Dry. No files renamed.')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
def fileprefix_argparse(args):
|
||||||
|
return fileprefix(
|
||||||
|
prefix=args.prefix,
|
||||||
|
sep=args.sep,
|
||||||
|
ctime=args.ctime,
|
||||||
|
dry=args.dry,
|
||||||
|
)
|
||||||
|
|
||||||
|
def main(argv):
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
|
||||||
|
parser.add_argument('prefix', nargs='?', default='')
|
||||||
|
parser.add_argument('--sep', dest='sep', default=' ')
|
||||||
|
parser.add_argument('--ctime', dest='ctime', action='store_true')
|
||||||
|
parser.add_argument('--dry', dest='dry', action='store_true')
|
||||||
|
parser.set_defaults(func=fileprefix_argparse)
|
||||||
|
|
||||||
|
args = parser.parse_args(argv)
|
||||||
|
args.func(args)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
main(sys.argv[1:])
|
Loading…
Reference in a new issue