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

[玩转系统] PowerShell - 检查文件是否存在

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

PowerShell - 检查文件是否存在


在脚本文件中进行基于文件的操作时,应遵循一种非常常见的做法来检查文件是否存在于指定位置。如果文件不存在,那么在尝试访问文件、删除文件、读取文件或任何类型的基于文件的操作时,它可能会在脚本中抛出异常。

在 PowerShell 中,在执行任何文件操作之前有不同的方法来检查文件是否存在以避免异常。

在本文中,我将通过示例向您解释如何在 PowerShell 中使用不同方式检查文件是否存在。

有四种不同的方法来检查文件是否存在,如下所示

  • 使用测试路径
  • 使用获取项目
  • 使用 Get-ChildItem
  • 使用 [System.IO.File]::Exists(文件)

让我们了解检查文件是否存在的每种方法

使用 Test-Path 检查文件是否存在

PowerShell Test-Path cmdlet 检查文件是否存在。如果文件存在则返回$True。如果路径或文件丢失或不存在,它将返回 $False。

测试路径 cmdlet 语法

Test-Path -Path <FilePath> -PathType Leaf

在上面的语法中,

FilePath - 文件位置

Leaf - 检查文件而不是目录

测试路径示例

让我们考虑一个示例来了解如何使用 PowerShell Test-Path cmdlet 来检查文件是否存在。

我们要从该位置删除 Employee.xlsx 文件。运行以下代码以了解 Test-Path cmdlet

$FileName = "D:\PowerShell\Employee.xlsx"
if (Test-Path $FileName) {
    Write-Host "File Exists"
    #Perform file based operation.. If file exists then delete file
    Remove-Item $FileName
}
else
{
    Write-Host "File Doesn't Exists"
}

在上面的 PowerShell 脚本中,

$FileName 变量包含 Employee.xlsx 文件路径

使用 Test-Path cmdlet,它首先检查该位置是否存在文件。如果文件存在,则使用Remove-Item cmdlet 删除文件。

如果文件不存在,则在控制台上打印消息“文件不存在”

酷提示:如何删除 PowerShell 中存在的文件夹!

PowerShell Get-Item 检查文件是否存在

PowerShell Get-Item cmdlet 用于获取指定位置的项目。您可以使用通配符(*)来获取item的所有内容。我们将了解如何使用 Get-Item cmdlet 来检查文件是否存在。

获取项目语法

Get-Item
   [-Path] <String[]>
   [-Filter <String>]
   [-Include <String[]>]
   [-Exclude <String[]>]
   [-Force]
   [-Credential <PSCredential>]
   [-Stream <String[]>]
   [<CommonParameters>]

在上面的Get-Item语法中,您可以使用不同的参数来获取特定的项目。

让我们了解一下,使用 Get-Item cmdlet 来检查 PowerShell 中是否存在文件。

获取项目示例

让我们考虑一个删除指定目录中可用的 Employee.xlsx 文件的示例。使用 PowerShell Get-Item cmdlet 通过以下命令检查文件是否存在

$FileName = "D:\PowerShell\Employee.xlsx"

if(Get-Item -Path $FileName -ErrorAction Ignore)
{
   
        Write-Host "File Exists"
        #Perform file based operation, if file exists then delete file
        Remove-Item $FileName
}
else
{
    Write-Host "File Doesn't Exists"
}

在上面的 PowerShell 脚本中,

$FileName 变量包含 Employee.xlsx 文件路径

使用 Get-Item cmdlet,它首先检查该位置是否存在文件。如果文件存在,则使用Remove-Item cmdlet 删除文件。

如果文件不存在,则在控制台上打印消息“文件不存在”。

酷提示:如何在 PowerShell 中获取文件创建日期!

PowerShell Get-ChildItem 检查文件是否存在

PowerShell Get-ChildItem cmdlet 用于从一个或多个指定位置获取项目或子项目。我们将通过示例了解如何使用 Get-ChildItem cmdlet 来检查文件是否存在。

您可以阅读此处以在 PowerShell 中获取不带扩展名的文件名。

获取子项语法

Get-ChildItem
   [[-Path] <string[]>]
   [[-Filter] <string>]
   [-Include <string[]>]
   [-Exclude <string[]>]
   [-Recurse]
   [-Depth <uint32>]
   [-Force]
   [-Name]
   [-Attributes <FlagsExpression[FileAttributes]>]
   [-FollowSymlink]
   [-Directory]
   [-File]
   [-Hidden]
   [-ReadOnly]
   [-System]
   [<CommonParameters>]

在上面的 Get-ChildItem 语法中,您可以使用不同的参数从 Path 参数指定的一个或多个特定位置获取项目。

让我们了解一下,使用 Get-ChildItem cmdlet 来检查 PowerShell 中是否存在文件。

酷提示:如何在PowerShell中使用base64编码文件!

获取子项示例

让我们考虑一个删除指定位置的 Employee.xlsx 文件的示例。使用 PowerShell Get-ChildItem cmdlet 通过以下命令检查文件是否存在

$FileName = "D:\PowerShell\Employee.xlsx"

if(Get-ChildItem -Path $FileName -ErrorAction Ignore)
{
    Write-Host "File Exists"
    #Perform file based operation
}
else
{
    Write-Host "File Doesn't Exists"
}

在上面的 PowerShell 脚本中,

$FileName 变量包含 Employee.xlsx 文件路径

使用 Get-ChildItem cmdlet,它首先检查该位置是否存在文件。如果文件存在,则使用Remove-Item cmdlet 删除文件。

如果文件不存在,则在控制台上打印消息“文件不存在”

-ErrorActionignore参数忽略检查文件存在操作时引发的异常。

酷提示:如何在 PowerShell 中查找大尺寸文件!

使用 [System.IO.File]::Exists() 方法

使用.Net类System.IO.FIle和Exists()方法,我们可以在执行任何操作之前检查文件是否存在。

让我们通过示例来了解 [System.IO.File]::Exists() 方法的使用。

例如,如果您想在指定位置创建一个新文件。在创建新文件之前,首先检查文件是否存在以避免以下错误

New-Item : The file 'D:\PowerShell\FileCreateExample.txt' already exists.
At line:2 char:1
+ New-Item $FileName -ItemType File
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (D:\PowerShell\FileCreateExample.txt:String) [New-Item], IOException
    + FullyQualifiedErrorId : NewItemIOError,Microsoft.PowerShell.Commands.NewItemCommand

[System.IO.File]::Exists() 方法将文件名作为输入,并根据文件存在与否返回 True 或 False。

$FileName = "D:\PowerShell\FileCreateExample.txt"

if([System.IO.File]::Exists($FileName))
{
    Write-Host "File Exists"
}
else
{
    Write-Host "File Doesn't Exists"
    New-Item $FileName -ItemType File
}

在上面的 PowerShell 脚本中,

$FileName - 包含文件名路径

使用 [System.IO.File]::Exists() 方法检查文件是否存在。如果文件存在则返回 true,否则返回 false。

如果文件不存在,您可以使用 New-Item cmdlet 创建新文件。

酷提示:如何在 PowerShell 中列出目录中的文件!

结论

在上面的文章中,我解释了使用 PowerShell 方式检查文件是否存在的四种不同方法。这些方法是 Test-Path 、 Get-Item 、 Get-ChildItem 和 [System.IO.File]::Exists()。

在执行任何基于文件的操作之前检查文件是否存在是一个很好的做法,以避免错误。使用文件存在检查,您可以处理错误和条件以进行文件操作。

您可以阅读有关使用 PowerShell mkdir 创建新目录或使用 New-Item cmdlet 创建目录(如果不存在)并将多个参数传递给函数来创建目录的更多信息。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

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

取消回复欢迎 发表评论:

关灯