Refresh and added usage tracking
This commit is contained in:
Executable
+59
@@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Usage:
|
||||
python3 recount_files.py <number> (<directory>)
|
||||
"""
|
||||
|
||||
import os
|
||||
from pathlib import Path
|
||||
import sys
|
||||
import re
|
||||
from termcolor import colored
|
||||
|
||||
def main():
|
||||
if len(sys.argv) == 2:
|
||||
dir = Path(os.getcwd())
|
||||
elif (len(sys.argv) == 3):
|
||||
dir = Path(sys.argv[2])
|
||||
else:
|
||||
print("Usage: recount_files.py <number> (<directory>)")
|
||||
sys.exit(1)
|
||||
import subprocess
|
||||
subprocess.run(["python", "/home/honney/.bin/tracker.py", "add", "recount_files"])
|
||||
|
||||
number = int(sys.argv[1])
|
||||
|
||||
pattern = re.compile(r'^\d+')
|
||||
|
||||
files = sorted(
|
||||
[f for f in dir.iterdir() if f.is_file() and pattern.match(f.name)],
|
||||
key=lambda f: float(f.stem)
|
||||
)
|
||||
|
||||
min_num = int(files[0].stem)
|
||||
max_len = max(len(files[-1].stem), 2)
|
||||
|
||||
for f in files:
|
||||
try:
|
||||
delta = int(f.stem)-min_num
|
||||
new_file_name = Path(f"{f.parent}/{delta+number:0{max_len}d}{f.suffix}")
|
||||
except:
|
||||
delta = float(f.stem)-min_num
|
||||
new_file_name = Path(f"{f.parent}/{delta+number:0{max_len+2}.1f}{f.suffix}")
|
||||
if not new_file_name.is_file():
|
||||
print(
|
||||
colored("Renamed: ", "green") +
|
||||
colored(str(f.parent), "cyan") +
|
||||
colored("[", "white") +
|
||||
colored(f.name, "red") +
|
||||
colored(" -> ", "white") +
|
||||
colored(new_file_name.name, "green") +
|
||||
colored("]", "white")
|
||||
)
|
||||
f.rename(new_file_name)
|
||||
else:
|
||||
print(colored(f"{new_file_name.name} already exists. Aborting", "red"))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user