Skip to content

Instantly share code, notes, and snippets.

@vizsumit
Created May 10, 2023 05:21
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vizsumit/1a84a0cbee6bd5f00bc1bfc0900a2b51 to your computer and use it in GitHub Desktop.
Save vizsumit/1a84a0cbee6bd5f00bc1bfc0900a2b51 to your computer and use it in GitHub Desktop.
This script generates .txt caption files from .jpg file names
@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
)
@glasgowdave
Copy link

Thanks for sharing

@oliverban
Copy link

Thank you so much, this is awesome!

@temo214
Copy link

temo214 commented Oct 13, 2023

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?

@CTimmerman
Copy link

CTimmerman commented Apr 15, 2024

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