LoginSignup
1
0

More than 5 years have passed since last update.

PowerShell でクリップボード中のスラッシュをバックスラッシュに置換

Last updated at Posted at 2016-12-23
  • クリップボード中の...

    1. フォワードスラッシュ / をバックスラッシュ \ にすべて置換する。
    2. バックスラッシュ \ をフォワードスラッシュ / にすべて置換する。
  • 両方含まれていた場合は、上記 1. に分類する(適当)。

コード

Add-Type -AssemblyName System.Windows.Forms
$clipText = [Windows.Forms.Clipboard]::GetText()
if ($clipText.Contains('/'))
{
    $replacedClipText = $clipText -replace '/', '\'
    [Windows.Forms.Clipboard]::SetText($replacedClipText)
}
elseif ($clipText.Contains('\'))
{
    $replacedClipText = $clipText -replace '\\', '/'
    [Windows.Forms.Clipboard]::SetText($replacedClipText)
}

実行結果

  1. 文字列 "C:\Path\to\the\File.ext" をクリップボードにセットした状態で上記のスクリプトを実行すると...

    C:/Path/to/the/File.ext
    

    ... がクリップボードにセットされる。

  2. この状態でもう一度、上記のスクリプトを実行すると...

    C:\Path\to\the\File.ext
    

    ... がクリップボードにセットされる。

Windows 7 で実行する際の注意点

Windows 7 (PSVersion=2.0) の場合、そのままだと動かない。
これは Clipboard クラスが、STA モード時でのみ使用可能であるのに対して、Windows 7 はデフォルトが MTA モードになっているため。
これを解決するには、PowerShell の起動時に -STA オプションを指定して、明示的に STA モードで動作させる必要がある。

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