LoginSignup
0

More than 1 year has passed since last update.

posted at

updated at

【Python3】ドラッグ&ドロップしたファイルにタイムスタンプをつけてリネームする

概要

PowerShell の .ps1 ファイルには、直接ファイルをドラッグ&ドロップできないので、ソースファイルとは別にショートカットを作成しなければなりませんでした。
ファイルだけで出来るように Python を使ってやってみました。

準備

まず、Python をインストールしておきます。

手順

以下のファイルを作成します。

Add-TimeStamp.py
import sys
import datetime
import pathlib
import os

daystr = datetime.date.today().strftime('%Y-%m-%d')
paths = sys.argv[1:]

for path in paths:
    file = pathlib.PurePath(path)
    new_file_path = os.path.join(file.parent, f"{file.stem}_{daystr}{file.suffix}")
    os.rename(path, new_file_path)

Add-TimeStamp.py ファイルとリネームしたいファイルを任意のフォルダに配置します。

使用方法

リネームしたいファイル (複数) を、作成した Add-TimeStamp.py ファイルにドラッグ&ドロップします。

Rename_Python.gif

参考サイト

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
What you can do with signing up
0