2019-06-12 05:41:31 +00:00
|
|
|
'''
|
|
|
|
Batch rename files by replacing the first argument with the second.
|
2020-10-02 05:45:26 +00:00
|
|
|
|
|
|
|
Note: If one of your arguments begins with a hyphen, it will confuse argparse
|
|
|
|
and it will say "the following arguments are required". You have to add "--"
|
|
|
|
before your from/to arguments, like this:
|
|
|
|
|
|
|
|
breplace -- " - Copy" "-copy"
|
2019-06-12 05:41:31 +00:00
|
|
|
'''
|
|
|
|
import argparse
|
|
|
|
import brename
|
|
|
|
import sys
|
|
|
|
|
2021-01-24 01:34:31 +00:00
|
|
|
from voussoirkit import pipeable
|
|
|
|
|
2019-06-12 05:41:31 +00:00
|
|
|
def breplace_argparse(args):
|
2021-01-24 01:34:31 +00:00
|
|
|
replace_from = ' '.join(pipeable.input(args.replace_from))
|
|
|
|
replace_to = ' '.join(pipeable.input(args.replace_to))
|
|
|
|
|
2020-09-05 20:49:41 +00:00
|
|
|
if args.regex:
|
2021-01-24 01:34:31 +00:00
|
|
|
command = f're.sub(r"{replace_from}", r"{replace_to}", x)'
|
2020-09-05 20:49:41 +00:00
|
|
|
else:
|
2021-01-24 01:34:31 +00:00
|
|
|
command = f'x.replace("{replace_from}", "{replace_to}")'
|
2019-12-10 20:59:20 +00:00
|
|
|
brename.brename(command, autoyes=args.autoyes, recurse=args.recurse)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('replace_from')
|
|
|
|
parser.add_argument('replace_to')
|
|
|
|
parser.add_argument('-y', '--yes', dest='autoyes', action='store_true', help='accept results without confirming')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--recurse', action='store_true', help='operate on subdirectories also')
|
|
|
|
parser.add_argument('--regex', action='store_true', help='treat arguments as regular expressions')
|
2019-06-12 05:41:31 +00:00
|
|
|
parser.set_defaults(func=breplace_argparse)
|
|
|
|
|
|
|
|
args = parser.parse_args(argv)
|
2020-02-09 01:18:50 +00:00
|
|
|
return args.func(args)
|
2019-06-12 05:41:31 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|