Powershell 特定部分の文字列を返す
# ファイルパスを関数に渡し、特定部分の文字列を返す
# 引 数: ファイルパス+ファイル名
# 戻り値: 該当文字列 マッチしない場合$nullを返す
function GetStr {
param ([string]$FilePath)
# 正規表現で特定の文字列を取り出す
$pattern = 'FileName_([^_]+).xlsx'
if ($FilePath -match $pattern) {
return $matches[1]
} else {
return $null
}
}
# 使用例
$FilePath = "C:\Temp\FileName_(文字列).xlsx"
$result = GetStr -FilePath $FilePath
Write-Output $result