如何使用 PowerShell 获取文件扩展名?

powershellmicrosoft technologiessoftware & coding

我们可以使用多种方法获取文件扩展名。首先,使用 [System.IO.Path] 类。

PS C:\> [System.IO.Path]::GetExtension("C:\temp\25Aug2020.txt")
.txt
PS C:\> [System.IO.Path]::GetExtension("C:\temp\azcopy.zip")
.zip

这是获取文件扩展名最简单的方法。其他方法, 也可以通过编程方式获取。

PS C:\> ((Split-Path "C:\Temp\azcopy.zip" -Leaf).Split('.'))[1]
zip
PS C:\> ((Split-Path "C:\Temp\25Aug2020.txt" -Leaf).Split('.'))[1]
txt

使用 Get-ChildItem

PS C:\> (Get-ChildItem C:\Temp\azcopy.zip).Extension
.zip
PS C:\> (Get-ChildItem C:\Temp\25Aug2020.txt).Extension
.txt

使用 Get-Item

PS C:\> (Get-Item C:\Temp\azcopy.zip).Extension
.zip

相关文章