LoginSignup
2
0

More than 1 year has passed since last update.

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

Last updated at Posted at 2022-03-17

概要

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

参考サイト

2
0
1

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
  3. You can use dark theme
What you can do with signing up
2
0