需要 trim 双扩展文件名的扩展名
Need to trim the extension for double extension filename
我有一些文件,名称如下,
输入可以是以下之一,
ABC.sample.tar.gz
XYZ.sample.tar.gz.Manifest
123.sample.zip
我有几个要求
- 需要 trim 完全关闭扩展名并将不带扩展名的文件名存储到变量中。
Sample output: $filename=ABC.sample or XYZ.sample or 123.sample
- 只需要 trim “.manifest”(如果它存在并存储到另一个变量中)。
代码:
$SURL = "ABC.sample.tar.gz.manifest"
$UDRname = [IO.Path]::GetFileNameWithoutExtension($SURL)
$UDRname
输出:ABC.sample.tar.gz
期望值:
- 必须删除实际的文件扩展名,如下所示
$filename=ABC.sample
$filename=XYZ.sample
$filename=123.sample
- 如果文件名中包含.manifest。
它必须 trim 治疗。
$trimed = XYZ.sample.tar.gz
您遇到的主要问题是 [IO.Path]::GetFileNameWithoutExtension($SURL)
,它不支持多个扩展的概念。
通过添加对多个点扩展的特殊考虑,
这是对你有用的东西。
在 $DoubleExtensions
中添加任何其他多重扩展(不包括清单扩展)以涵盖您可能遇到的其他情况。
考虑以下因素:
Function Get-FileInfos($FullName) {
$DoubleExtensions = @('.tar.gz')
$Manifestext = '.manifest'
$IsManifest = $FullName.EndsWith($Manifestext)
$Extension, $TrimmedName = $null
$NameToProcess = $FullName
# Process manifest extention
if ($IsManifest) {
$TrimmedName = [IO.Path]::GetFileNameWithoutExtension($FullName)
$NameToProcess = $TrimmedName
}
# Process double extensions
Foreach ($Ext in $DoubleExtensions) {
if ($NameToProcess.EndsWith($Ext)) {
if ($IsManifest -eq $false) {
$Extension = $Ext
$ShortName = $NameToProcess.Substring(0, $NameToProcess.Length - $ext.Length)
}
else {
$Extension = $Ext
$ShortName = $NameToProcess
}
break
}
}
# Process normal extensions
if ($null -eq $Extension ) {
if ($IsManifest -eq $false) {
$Extension = [io.Path]::GetExtension($NameToProcess)
$ShortName = [IO.Path]::GetFileNameWithoutExtension($NameToProcess)
}
else {
$ShortName = $NameToProcess
}
}
# If manifest, extension should be .manifest without original file extension
if ($IsManifest) { $Extension = $Manifestext }
return [PSCustomObject]@{
FullName = $FullName
TrimmedName = $TrimmedName
ShortName = $ShortName
Extension = $Extension
}
}
样本测试
$TestList = @(
'ABC.sample.tar.gz.manifest'
'DEF.sample.tar.gz'
'GHI.Sample.manifest'
'JKL.exe'
)
$TestList | foreach { Get-FileInfos -FullName $_ }
结果
我有一些文件,名称如下,
输入可以是以下之一,
ABC.sample.tar.gz
XYZ.sample.tar.gz.Manifest
123.sample.zip
我有几个要求
- 需要 trim 完全关闭扩展名并将不带扩展名的文件名存储到变量中。
Sample output: $filename=ABC.sample or XYZ.sample or 123.sample
- 只需要 trim “.manifest”(如果它存在并存储到另一个变量中)。
代码:
$SURL = "ABC.sample.tar.gz.manifest"
$UDRname = [IO.Path]::GetFileNameWithoutExtension($SURL)
$UDRname
输出:ABC.sample.tar.gz
期望值:
- 必须删除实际的文件扩展名,如下所示
$filename=ABC.sample
$filename=XYZ.sample
$filename=123.sample
- 如果文件名中包含.manifest。 它必须 trim 治疗。
$trimed = XYZ.sample.tar.gz
您遇到的主要问题是 [IO.Path]::GetFileNameWithoutExtension($SURL)
,它不支持多个扩展的概念。
通过添加对多个点扩展的特殊考虑, 这是对你有用的东西。
在 $DoubleExtensions
中添加任何其他多重扩展(不包括清单扩展)以涵盖您可能遇到的其他情况。
考虑以下因素:
Function Get-FileInfos($FullName) {
$DoubleExtensions = @('.tar.gz')
$Manifestext = '.manifest'
$IsManifest = $FullName.EndsWith($Manifestext)
$Extension, $TrimmedName = $null
$NameToProcess = $FullName
# Process manifest extention
if ($IsManifest) {
$TrimmedName = [IO.Path]::GetFileNameWithoutExtension($FullName)
$NameToProcess = $TrimmedName
}
# Process double extensions
Foreach ($Ext in $DoubleExtensions) {
if ($NameToProcess.EndsWith($Ext)) {
if ($IsManifest -eq $false) {
$Extension = $Ext
$ShortName = $NameToProcess.Substring(0, $NameToProcess.Length - $ext.Length)
}
else {
$Extension = $Ext
$ShortName = $NameToProcess
}
break
}
}
# Process normal extensions
if ($null -eq $Extension ) {
if ($IsManifest -eq $false) {
$Extension = [io.Path]::GetExtension($NameToProcess)
$ShortName = [IO.Path]::GetFileNameWithoutExtension($NameToProcess)
}
else {
$ShortName = $NameToProcess
}
}
# If manifest, extension should be .manifest without original file extension
if ($IsManifest) { $Extension = $Manifestext }
return [PSCustomObject]@{
FullName = $FullName
TrimmedName = $TrimmedName
ShortName = $ShortName
Extension = $Extension
}
}
样本测试
$TestList = @(
'ABC.sample.tar.gz.manifest'
'DEF.sample.tar.gz'
'GHI.Sample.manifest'
'JKL.exe'
)
$TestList | foreach { Get-FileInfos -FullName $_ }
结果