Windowsのファイルにコメントする方法を考える2 – 20250322

ブログ コンピュータ
ブログ

ファイルのコメントを保存するテキストファイルをnotepad.exeで開くスクリプトを作成しました。

ソースコード

<#
.SYNOPSIS
概要を保存するテキストファイルをノートパッドで開く

.DESCRIPTION
概要を保存するテキストファイルをノートパッドで開く

.EXAMPLE
Edit-DescriptionText.ps1 -Path ファイル名

.PARAMETER $Path
ファイルまたはディレクトリ

#>

param(
    [string]
    $Path,
    [switch]
    $Help
)

if ($Help) {
    Get-Help $MyInvocation.MyCommand.Path
    Exit 1
}

$EDITOR = "notepad.exe"
$DEFAULT_FILE = "readme.txt"
$SUFFIX = ".txt"

# パスが存在しない場合終了
if (-not(Test-Path -Path $Path)) {
    Exit
}


$name = Split-Path $Path -Leaf

# ディレクトリか?
if (Test-Path -Path $Path -PathType Container) {
    $targetPath = (Join-Path $Path $DEFAULT_FILE)
    if (-not(Test-Path $targetPath))
    {
        Set-Content -Path $targetPath -Value "${name}"
    }
} else {
    $targetPath = ($Path + $SUFFIX)
    if (-not(Test-Path $targetPath))
    {
        Set-Content -Path $targetPath -Value "${name}"
    }
}
. $EDITOR $targetPath

使い方

スクリプトは、第一引数のファイルのパスに.txtを追加してノートパッドで開きます。
フォルダの場合readme.txtというファイル名でノートパッドで開きます。
テキストファイルの初期値としてファイル名またはフォルダ名をセットするようにしてあります。

コメント