mieki256's diary



2025/05/06(火) [n年前の日記]

#1 [python] 連番リネームするPythonスクリプトを作成してもらった

画像生成AI Stable Diffusion web UI で生成した画像群は、00123-456789.png といった感じの、先頭に5桁の数字とハイフン("-")がついているファイル名になっている。リネームして順番を整理したいのだけど、二画面ファイラー「あふw」上の拡張リネームでは ―― 正規表現による置換や、連番指定による置換では、ちょっと実現が難しい気がする。ここは何かしらのスクリプトでも書かないとダメだろうか。

そこで、AIの Google Gemini 2.0 に、連番でリネームするPythonスクリプトを作成してもらった。先頭に5桁の数字とハイフン("-")がついている複数のファイル名に対して、初期値と増分を指定して、5桁部分を連番でリネームする、といった処理。

renseqn.py
"""
Renames the specified file paths sequentially.
The target filenames must start with a 5-digit number and a hyphen.

Windows10 x64 22H2 + Python 3.10.10 64bit
"""

import os
import re
import argparse


def rename_files_sequentially(file_paths, initial_value, step_value):
    """
    Renames the specified file paths sequentially.
    The target filenames must start with a 5-digit number and a hyphen.
    The script will exit with an error if a target filename already exists.
    Rename information is stored in a dictionary before the actual renaming.
    Temporary renaming is removed. Existence check is done before final rename.
    """
    rename_info = {}
    pattern = re.compile(r"^(\d{5}-)(.*)$")

    for file_path in file_paths:
        filename = os.path.basename(file_path)
        match = pattern.match(filename)
        if match:
            prefix, suffix = match.groups()
            rename_info[file_path] = {
                "old_path": file_path,
                "old_name": filename,
                "suffix": suffix,
                "new_name": None,
            }
        else:
            print(
                f"Warning: '{filename}' is not targeted for renaming (filename does not match the pattern)."
            )

    if not rename_info:
        print("No target files found.")
        return

    # Sort rename_info based on the original file paths to maintain order
    sorted_filepaths = sorted(rename_info.keys())

    # Check for existing target filenames
    existing_files = []
    counter = initial_value
    for old_filepath in sorted_filepaths:
        info = rename_info[old_filepath]
        new_name = f"{counter:05d}-{info['suffix']}"
        target_path = os.path.join(os.path.dirname(old_filepath), new_name)
        if os.path.exists(target_path):
            existing_files.append(os.path.basename(target_path))
        info["new_name"] = new_name
        counter += step_value

    if existing_files:
        print("Error: The following target filenames already exist:")
        for f in existing_files:
            print(f"- {f}")
        print("Exiting script to avoid overwriting files.")
        return

    # Final renaming phase
    for old_filepath in sorted_filepaths:
        info = rename_info[old_filepath]
        final_path = os.path.join(os.path.dirname(old_filepath), info["new_name"])
        try:
            os.rename(info["old_path"], final_path)
            print(f"Renamed '{info['old_name']}' to '{info['new_name']}'.")
        except OSError as e:
            print(
                f"Error: Failed to rename '{info['old_name']}' to '{info['new_name']}': {e}"
            )


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description="Renames the specified file paths sequentially while keeping part of the original filename."
    )
    parser.add_argument(
        "file_paths", nargs="+", help="Specify one or more file paths to rename."
    )
    parser.add_argument(
        "-i",
        "--initial",
        type=int,
        help="Specify the initial value of the sequence.",
    )
    parser.add_argument(
        "-s",
        "--step",
        type=int,
        help="Specify the step value of the sequence.",
    )

    args = parser.parse_args()

    if args.initial is None:
        args.initial = int(input("Input initial value of the sequence : "))

    if args.step is None:
        args.step = int(input("Input step value of the sequence : "))

    print(f"Init value : {args.initial}")
    print(f"Steps : {args.step}")

    rename_files_sequentially(args.file_paths, args.initial, args.step)

    _ = input("\nHit ENTER key. ")

使い方は以下。
> py renseqn.py --help
usage: renseqn.py [-h] [-i INITIAL] [-s STEP] file_paths [file_paths ...]

Renames the specified file paths sequentially while keeping part of the original filename.

positional arguments:
  file_paths            Specify one or more file paths to rename.

options:
  -h, --help            show this help message and exit
  -i INITIAL, --initial INITIAL
                        Specify the initial value of the sequence.
  -s STEP, --step STEP  Specify the step value of the sequence.

python renseqn.py -i 100 -s 2 C:\temp\00001-123456.png C:\temp\00002-123456.png

Windows10 x64 22H2 + Python 3.10.10 64bit上で、二画面ファイラーの「あふw」から呼び出して動作確認したところ、ちゃんとそれらしく動いてくれた。あふwから呼び出されるメニュー内には以下を記述。

"r 連番リネーム [0-9]{5}-*"         python D:\hoge\fuga\renseqn.py $MF


このくらいのスクリプトなら、AIと何度かやり取りするだけで作成してもらえるあたり、なんだか凄いなと…。

以上です。

過去ログ表示

Prev - 2025/05 - Next
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

カテゴリで表示

検索機能は Namazu for hns で提供されています。(詳細指定/ヘルプ


注意: 現在使用の日記自動生成システムは Version 2.19.6 です。
公開されている日記自動生成システムは Version 2.19.5 です。

Powered by hns-2.19.6, HyperNikkiSystem Project