在 Powershell v6 中,split-path 具有-Extension
访问文件扩展名的参数,例如:
$pathToResume = "R:\Work\cover.letter_resume_base.odt"
$extension = Split-Path -Extension $pathtoResume
Write-Output $extension # Will print .odt
但是,Powershell 3 没有提供-Extension
参数,但我想出了这个解决方案:
# Notice the period between cover and letter
$pathToResume = "R:\Work\cover.letter_resume_base.odt"
$pathToResumeLeaf = Split-Path -Leaf $pathToResume
$pathToResumeLeafArray = $pathToResumeLeaf.Split(".")
$fileExtension = $pathToResumeLeafArray | Select-Object -Last 1
Write-Output $fileExtension # Will print odt
我仍然得到文件扩展名,但没有句点。无论文件名中有多少个句点或数组的长度,我都会得到相同的输出。
我想不出任何需要期限的情况。如果我想打印带有扩展名的句点,我可以在使用时轻松添加它Write-Output
或[string]::format()
当不可用Select-Object
时,正如我在上面显示的那样是可行的解决方案吗?-Extension