当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何在 PowerShell 中从文件名获取文件扩展名?

作者:精品下载站 日期:2024-12-14 05:10:58 浏览:14 分类:玩电脑

如何在 PowerShell 中从文件名获取文件扩展名?


当我需要使用 PowerShell 从文件名中提取文件扩展名时,我正在从事一个自动化项目。我尝试了不同的方法,在本教程中,我将探索使用完整脚本从 PowerShell 中的文件名检索文件扩展名的各种方法。

要在 PowerShell 中从文件名获取文件扩展名,您可以使用 [System.IO.Path] 类中的 Get-Extension 方法。例如,$fileExtension=[System.IO.Path]::GetExtension(‘C:\example\report.xlsx’) 会将字符串‘.xlsx’存储在 $fileExtension 变量中。该方法简单有效,可以提取表示文件类型的后缀。

在 PowerShell 中从文件名获取文件扩展名

文件扩展名是文件名末尾的后缀,指示文件类型。它与主文件名之间用点分隔。例如,在“document.pdf”中,“.pdf”是表明该文件是 PDF 文档的文件扩展名。

PowerShell 提供了不同的方法和 cmdlet 来从文件名获取文件扩展名。

方法1:使用System.IO.Path类

在 PowerShell 中获取文件扩展名的最简单方法之一是使用 [System.IO.Path] 类。此类提供用于创建、移动和枚举目录和文件的静态方法。

下面是一个示例脚本,说明如何使用 [System.IO.Path] 类提取文件扩展名:

# Define the full path of the file
$filePath = "C:\MyFolder\users.xlsx"

# Extract the extension from the file path
$fileExtension = [System.IO.Path]::GetExtension($filePath)

# Display the file extension
Write-Host "The file extension is: $fileExtension"

在上面的PowerShell脚本中,GetExtension()方法用于从文件的完整路径获取文件扩展名。

在我使用 VS Code 执行脚本后,您可以在下面的屏幕截图中看到输出。

[玩转系统] 如何在 PowerShell 中从文件名获取文件扩展名?

方法 2:使用 Get-ChildItem 和 Select-Object

PowerShell 的 Get-ChildItem cmdlet 是另一种获取文件详细信息的方法。与Select-Object cmdlet 结合使用,您可以隔离并显示文件扩展名。

以下是如何使用这些 cmdlet 来获取文件扩展名:

# Define the directory path
$directoryPath = "C:\MyFolder"

# Get all files in the directory and select only the extension property
$fileExtensions = Get-ChildItem -Path $directoryPath | Select-Object Extension

# Display the file extensions
$fileExtensions | ForEach-Object { Write-Host "Found file extension: $($_.Extension)" }

该脚本检索指定目录中的所有文件,然后使用 Select-Object 只选取每个文件的扩展名属性。

查看下面屏幕截图中的输出;它显示上述文件夹中的所有文件扩展名。未提及的扩展名是子文件夹。

[玩转系统] 如何在 PowerShell 中从文件名获取文件扩展名?

方法 3:使用分割路径

PowerShell 中的 Split-Path cmdlet 旨在操作给定位置的路径和叶子。您可以使用它来分割路径和文件名,隔离扩展名。

使用 Split-Path 的示例脚本:

# Define the full file path
$filePath = "C:\MyFolder\Customers.xlsx"

# Use Split-Path to separate the file name and extension
$fileNameAndExtension = Split-Path -Path $filePath -Leaf

# Extract the extension from the full file name
$fileExtension = $fileNameAndExtension -replace '.*\.'

# Display the file extension
Write-Host "The file extension is: $fileExtension"

在此脚本中,Split-Path 获取路径的叶子(文件名和扩展名),然后使用正则表达式删除点之前的所有内容,只留下扩展名。

在我使用 VS code 执行脚本后,您可以在下面的屏幕截图中看到输出。

[玩转系统] 如何在 PowerShell 中从文件名获取文件扩展名?

方法四:使用正则表达式

PowerShell 还可以使用正则表达式从文件名中提取文件扩展名。

这是一个使用正则表达式查找扩展名的脚本:

# Define the filename
$fileName = "archive.tar.gz"

# Use regex to extract the file extension
if ($fileName -match "\.(?<Extension>[^.]+)$") {
    $fileExtension = $Matches['Extension']
    Write-Host "The file extension is: $fileExtension"
} else {
    Write-Host "No file extension found."
}

此脚本使用 -match 运算符来检查文件名是否与文件扩展名的模式匹配。如果找到匹配项,它将使用 $Matches 自动变量提取扩展名。

结论

我希望您现在必须学习如何在 PowerShell 中从文件名获取文件扩展名。我已经解释了从文件名获取文件扩展名的不同方法,例如使用 [System.IO.Path] 类或 Get-ChildItem cmdlet,通过使用 Split -路径,或者使用正则表达式等,希望有帮助。

您可能还喜欢:

  • 如何在 PowerShell 中按名称查找文件?
  • 在 PowerShell 中用日期重命名文件
  • 如何在 PowerShell 中仅列出文件名?
  • 在 PowerShell 中复制和重命名文件

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯