param(
[Parameter(Mandatory=$true)]
[string]$Path1,
[Parameter(Mandatory=$true)]
[string]$Path2
)
# WinMergeのパスを指定
$winmergePath = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2
このスクリプトは、2つの必須パラメータ($Path1
および $Path2
)を受け取ります。これらは比較する2つのファイルまたはディレクトリのパスです。
スクリプトは次に、WinMergeの実行可能ファイルへのパスを指定します。このパスはあなたのシステムによって異なる可能性があるため、必要に応じて調整してください。
最後に、スクリプトはWinMergeを起動し、$Path1
と$Path2
を比較します。-e
オプションはWinMergeが終了するまでスクリプトの実行を停止します。-ub
オプションはバックアップファイルを作成しないことを指定します。-dl "Left"
と-dr "Right"
オプションは、比較ウィンドウの左側と右側のパネルに表示されるタイトルを設定します。
このスクリプトを.ps1ファイルに保存し、PowerShellから実行することができます。例えば、スクリプトをcompare-files.ps1
という名前で保存し、C:\\path\\to\\file1
とC:\\path\\to\\file2
を比較する場合、次のコマンドを実行します:
.\\compare-files.ps1 -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
`
了解しました。その場合、スクリプトは次のようになります:
param(
[Parameter(Mandatory=$true)]
[string]$Path1,
[Parameter(Mandatory=$true)]
[string]$Path2
)
# WinMergeのパスを指定
$winmergePath64 = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
$winmergePath32 = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
# 存在するWinMergeのパスを選択
$winmergePath = $null
if (Test-Path $winmergePath64) {
$winmergePath = $winmergePath64
} elseif (Test-Path $winmergePath32) {
$winmergePath = $winmergePath32
}
if ($winmergePath -eq $null) {
Write-Output "WinMerge not found"
exit 1
}
# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2
このスクリプトは、最初に64ビット版のWinMergeがインストールされているか確認します。64ビット版が存在しない場合は、32ビット版が存在するか確認します。どちらも存在しない場合、スクリプトはエラーメッセージを出力して終了します。
このスクリプトを.ps1ファイルに保存し、PowerShellから実行することができます。例えば、スクリプトをcompare-files.ps1
という名前で保存し、C:\\path\\to\\file1
とC:\\path\\to\\file2
を比較する場合、次のコマンドを実行します:
.\\compare-files.ps1 -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
`
了解しました。以下のように修正したスクリプトを提供します:
param(
[Parameter(Mandatory=$true)]
[string]$Path1,
[Parameter(Mandatory=$true)]
[string]$Path2
)
# WinMergeのパスを辞書で指定
$winmergePaths = @{
"64bit" = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
"32bit" = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
}
# 存在するWinMergeのパスを選択
$winmergePath = $null
foreach ($path in $winmergePaths.Values) {
if (Test-Path $path) {
$winmergePath = $path
break
}
}
if ($winmergePath -eq $null) {
Write-Output "WinMerge not found"
exit 1
}
# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2
このスクリプトは、WinMergeのパスを辞書で指定し、foreach
ループを使用して各パスが存在するかどうかを確認します。存在するパスが見つかった場合、そのパスがWinMergeの実行パスとして選択され、ループは即座に終了します。どのパスも存在しない場合、スクリプトはエラーメッセージを出力して終了します。
了解しました。PowerShellのパイプライン機能を活用すると、スクリプトは以下のようになります:
param(
[Parameter(Mandatory=$true)]
[string]$Path1,
[Parameter(Mandatory=$true)]
[string]$Path2
)
# WinMergeのパスを辞書で指定
$winmergePaths = @{
"64bit" = "C:\\Program Files\\WinMerge\\WinMergeU.exe"
"32bit" = "C:\\Program Files (x86)\\WinMerge\\WinMergeU.exe"
}
# 存在するWinMergeのパスを選択
$winmergePath = $winmergePaths.Values | Where-Object { Test-Path $_ } | Select-Object -First 1
if ($winmergePath -eq $null) {
Write-Output "WinMerge not found"
exit 1
}
# WinMergeを実行
& $winmergePath -e -ub -dl "Left" -dr "Right" $Path1 $Path2
このスクリプトは、Where-Object
コマンドレットを使ってパスが存在するかをフィルタリングし、Select-Object -First 1
で最初に見つかったパスを選択しています。これにより、foreach
ループを使わずに同じ結果を得ることができます。
この.ps1ファイルをコマンドプロンプトから実行するには、以下の手順に従います。
cmdCopy codepowershell -Command "Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser"
powershell -File "C:\\path\\to\\your\\script.ps1" -Path1 "C:\\path\\to\\file1" -Path2 "C:\\path\\to\\file2"
C:\\path\\to\\your\\script.ps1
はスクリプトファイルのパスであり、C:\\path\\to\\file1
とC:\\path\\to\\file2
は比較するファイルまたはディレクトリのパスです。これらを適切なパスに置き換えてください。