contrib: add bulkrename.py

This commit is contained in:
Leon Henrik Plickat 2022-09-05 07:19:31 +02:00
parent a61435e8fc
commit 852b445f90

37
contrib/bulkrename.py Executable file
View file

@ -0,0 +1,37 @@
#!/bin/env python
import os
import subprocess
tmpname = ".bulkrename-tmp"
files = os.listdir(".")
tmpfile = open(tmpname, "w")
tmpfile.write("[ ] <-- mark with x to confirm\n")
for f in files:
tmpfile.write(f + "\n")
tmpfile.close()
editor = os.environ.get("EDITOR")
subprocess.call([editor, tmpname])
tmpfile = open(tmpname, "r")
input = tmpfile.readlines()
tmpfile.close()
if len(input) != len(files) + 1:
os.remove(tmpname)
exit(1)
if input[0][1] != 'x':
os.remove(tmpname)
exit(1)
print("starting bulk rename")
for i in range(len(files)):
original = files[i]
new = input[i + 1].rstrip()
if (original != new):
print("rename: \"" + original + "\" -> \"" + new + "\"")
os.rename(original, new)
print("done")
os.remove(tmpname)