Commit b11d6671 authored by Dennis Baurichter's avatar Dennis Baurichter

batch-rename: Add --help and safeguards

parent cbcfb835
......@@ -23,6 +23,18 @@ import shutil
import sys
import tempfile
if len(sys.argv) > 1:
sys.stderr.write(
"batch-rename\n"
"Invoke vim on a temporary file containing all file names in the current\n"
"directory. The names can be edited so the files will be renamed\n"
"accordingly.\n"
"\x1b[33;1mWarning\x1b[30;0m: This tool ignores naming schemes! You might\n"
"want to use rename or apply-rename instead!\n"
"\x1b[33;1mWarning\x1b[30;0m: This tool can also rename symlinks, but\n"
"will not change their targets.\n")
sys.exit(1)
files=[]
for fn in os.listdir("."):
......@@ -47,14 +59,22 @@ if len(new_filenames)!=len(files):
"\x1b[31;1mError\x1b[30;0m: batch rename failed: mismatching line count\n")
sys.exit(1)
for idx,src in enumerate(files):
src_base=os.path.splitext(os.path.basename(src))[0]
dst_base=new_filenames[idx]
if src_base==dst_base: continue
print("%s \x1b[34;1m->\x1b[30;0m %s"%(src_base,dst_base))
dst=os.path.join(os.path.dirname(src),dst_base+os.path.splitext(src)[1])
shutil.move(src,dst)
for execute in False, True:
if not execute:
print("These are the files that would be renamed:")
for idx,src in enumerate(files):
src_base=os.path.splitext(os.path.basename(src))[0]
dst_base=new_filenames[idx]
if src_base==dst_base: continue
if not execute:
print("%s \x1b[34;1m->\x1b[30;0m %s"%(src_base,dst_base))
else:
dst=os.path.join(os.path.dirname(src),dst_base+os.path.splitext(src)[1])
shutil.move(src,dst)
if not execute:
input("Press Enter to continue, Ctrl+C to abort.\n")
os.unlink(fnEdit)
......
......@@ -326,14 +326,22 @@ if fBatchRename and len(batch_rename_files)>0:
if len(new_filenames)!=len(batch_rename_files):
log("error","batch rename failed: mismatching line count","31;1")
else:
for idx,src in enumerate(batch_rename_files):
src_base=os.path.splitext(os.path.basename(src))[0]
dst_base=new_filenames[idx]
if src_base==dst_base: continue
log("batch renaming","%s \x1b[34;1m->\x1b[30;0m %s"%(src_base,dst_base),"34;1")
dst=os.path.join(os.path.dirname(src),dst_base+os.path.splitext(src)[1])
shutil.move(src,dst)
for execute in False, True:
if not execute:
log("batch renaming","These are the files that would be renamed:")
for idx,src in enumerate(batch_rename_files):
src_base=os.path.splitext(os.path.basename(src))[0]
dst_base=new_filenames[idx]
if src_base==dst_base: continue
if not execute:
log("batch renaming","%s \x1b[34;1m->\x1b[30;0m %s"%(src_base,dst_base),"34;1")
else:
dst=os.path.join(os.path.dirname(src),dst_base+os.path.splitext(src)[1])
shutil.move(src,dst)
if not execute:
input("Press Enter to continue, Ctrl+C to abort.\n")
if len(renaming_data)>0 and not fQuiet:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment