Created
May 10, 2023 05:21
-
-
Save vizsumit/1a84a0cbee6bd5f00bc1bfc0900a2b51 to your computer and use it in GitHub Desktop.
This script generates .txt caption files from .jpg file names
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@echo off | |
for %%i in (*.jpg) do ( | |
set filename=%%i | |
set caption_filename=%%~ni.txt | |
set caption=%%~ni | |
setlocal EnableDelayedExpansion | |
for /f "tokens=1 delims=()" %%a in ("!caption!") do ( | |
set caption=%%a | |
) | |
for /f "tokens=* delims=0123456789()" %%b in ("!caption!") do ( | |
set caption=%%b | |
) | |
echo !caption! > %%~ni.txt | |
) |
Thank you so much, this is awesome!
In every .txt file, only 'ECHO is off.' is written, and when the file is run as an administrator, nothing happens. Where is the problem?
In every .txt file, only 'ECHO is off.' is written, and when the file is run as an administrator, nothing happens. Where is the problem?
Same, so i wrote my own version in Python:
"""Caption generator from file names.
Based on https://gist.github.com/vizsumit/1a84a0cbee6bd5f00bc1bfc0900a2b51
By Cees Timmerman, 2024-04-15.
"""
from pathlib import Path
import re
import sys
path_str = sys.argv[1] if len(sys.argv) > 1 else ""
path = Path(path_str)
print(
f"Captioning {str(path.absolute())}"
if path.exists()
else f"Path not found: {path_str}"
)
files = (
p.resolve()
for p in path.glob("*") # "**/*" to include subfolders
if p.suffix in {".jpg", ".jpeg", ".webp", ".avif", ".jxl"}
)
for file in files:
print(file)
with open(file.name + ".txt", "w", encoding="utf8") as f:
print(file.name)
lines = re.split("[0-9()]+", file.name)
print(lines)
f.write("\n".join(lines))
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks for sharing