2020-12-08 04:31:10 +00:00
|
|
|
'''
|
|
|
|
Given a target string to replace, rename files by prompting the user for input.
|
|
|
|
'''
|
|
|
|
import argparse
|
2020-02-28 04:53:00 +00:00
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
2021-01-14 01:47:07 +00:00
|
|
|
from voussoirkit import pathclass
|
2020-12-08 04:31:10 +00:00
|
|
|
from voussoirkit import pipeable
|
2021-01-14 01:47:07 +00:00
|
|
|
from voussoirkit import spinal
|
2020-10-26 03:13:59 +00:00
|
|
|
|
2020-12-08 04:31:10 +00:00
|
|
|
@pipeable.ctrlc_return1
|
2020-10-26 03:13:59 +00:00
|
|
|
def inputrename_argparse(args):
|
2021-01-14 01:47:07 +00:00
|
|
|
if args.recurse:
|
2021-05-18 00:00:51 +00:00
|
|
|
files = (file for file in spinal.walk('.') if args.keyword in file.basename)
|
2021-01-14 01:47:07 +00:00
|
|
|
else:
|
2021-01-15 22:11:50 +00:00
|
|
|
files = (file for file in pathclass.cwd().listdir() if args.keyword in file.basename)
|
2020-10-26 03:13:59 +00:00
|
|
|
prev = None
|
|
|
|
for file in files:
|
2021-09-24 06:42:34 +00:00
|
|
|
pipeable.stderr(file.relative_path)
|
2020-10-26 03:13:59 +00:00
|
|
|
this = input('> ')
|
|
|
|
if this == '' and prev is not None:
|
|
|
|
this = prev
|
|
|
|
if this:
|
2021-01-14 01:47:07 +00:00
|
|
|
new_name = file.basename.replace(args.keyword, this)
|
|
|
|
new_name = file.parent.with_child(new_name)
|
2021-12-03 03:34:28 +00:00
|
|
|
os.rename(file, new_name)
|
2020-10-26 03:13:59 +00:00
|
|
|
prev = this
|
|
|
|
|
2021-09-24 06:42:34 +00:00
|
|
|
return 0
|
|
|
|
|
2020-10-26 03:13:59 +00:00
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('keyword')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--recurse', action='store_true')
|
2020-10-26 03:13:59 +00:00
|
|
|
parser.set_defaults(func=inputrename_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
|
|
|
return args.func(args)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2020-12-08 04:31:10 +00:00
|
|
|
raise SystemExit(main(sys.argv[1:]))
|