Refresh and added usage tracking

This commit is contained in:
Hannes
2026-04-24 00:47:51 +02:00
parent 8519d9f62e
commit 477b1bf985
45 changed files with 726 additions and 166 deletions
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
"""
rename_to_first_n.py
Usage:
python3 rename_to_first_n.py <number> <directory>
Renames each file in <directory> so its name becomes the first
four characters of the original filename (before the extension).
Keeps the file extension and avoids overwriting existing files.
"""
import os
import sys
from pathlib import Path
def main():
if len(sys.argv) < 2:
print("Usage: rename_to_first4.py <number> <directory>")
sys.exit(1)
import subprocess
subprocess.run(["python", "/home/honney/.bin/tracker.py", "add", "rename_to_first_n"])
try:
length = int(sys.argv[1])
except:
raise ValueError(f"Error: '{sys.argv[1]}' is not a number.")
try:
directory = Path(sys.argv[2]) if len(sys.argv) > 2 else Path(os.getcwd())
except:
raise ValueError(f"Error: '{sys.argv[2] if len(sys.argv) > 2 else os.getcwd()}' is not a directory.")
if not directory.is_dir():
raise ValueError(f"Error: '{directory}' is not a directory.")
for f in directory.iterdir():
if not f.is_file():
continue
original_stem = f.stem
if len(original_stem) < length:
print(f"Skipping {f.name} because it is less than {length} characters.")
continue
stem = original_stem[:length] # first n letters of filename (no extension)
new_name = f"{stem}{f.suffix}"
new_path = directory / new_name
# avoid overwriting existing files
if new_path.exists():
print(f"File {new_name} already exists. Skipping.")
continue
print(f"Renaming: {f.name} -> {new_name}")
f.rename(new_path)
print("✅ Done renaming all files.")
if __name__ == "__main__":
main()