1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

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

Last updated at Posted at 2022-03-16

概要

こちらの記事を見て、PowerShell を使ってできないか試してみました。

作成

PowerShell の .ps1 ファイルには、直接ファイルをドラッグ&ドロップできないのでショートカットを利用します。

手順

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

Add-TimeStamp.ps1
using namespace System.IO;

<#
.Synopsis
   ファイル名の末尾にタイムスタンプを追加します。
.DESCRIPTION
   ファイル名の末尾にタイムスタンプを追加した名前でリネームします。
.EXAMPLE
   dir 'C:\Works' -File | tstmp
#>
function Add-TimeStamp {
    [CmdletBinding()]
    [Alias('tstmp')]
    [OutputType([FileInfo])]
    Param (
        # ファイルパス
        [Parameter(Mandatory, ValueFromPipeline, Position=0)]
        [FileInfo] $Path
    ) Begin {
        $dateStr = (Get-Date -f yyyy-MM-dd)
    } Process {
        rni $Path ("{0}_$dateStr{1}" -f $Path.BaseName, $Path.Extension) -PassThru
    } End {
    }
}

$args | tstmp

Add-TimeStamp.ps1 へのショートカットを作成して、[リンク先(T):] に以下のコマンドを設定し、[作業フォルダー(S):] を空にしておきます。
※特定の作業フォルダーに ps1 ファイルを配置して使います場合は、[作業フォルダー(S):] を指定する

image.png

powerShell -ExecutionPolicy RemoteSigned -File Add-TimeStamp.ps1

Add-TimeStamp.ps1 ファイルとそのショートカットを任意のフォルダに配置します。

使用方法

リネームしたいファイル (複数) を、作成したショートカットにドラッグ&ドロップします。

Rename.gif

追記

ファイルだけで出来るように Python を使ってやってみました。

参考サイト

1
1
0

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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?