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

[玩转系统] 如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例

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

如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例


[玩转系统] 如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例

PowerShell(如果文件存在)简介

PowerShell 中存在需要创建新文件的情况。但是,在创建文件之前,建议检查文件是否存在,以便现有文件不会被覆盖,或者如果不正确,可能会引发错误。为了在 PowerShell 中实现这一点,本文将介绍多种方法。最流行的方法是使用 Test-Path cmdlet。其他方法包括 Get-Item 和 Get-ChildItem。

Hadoop、数据科学、统计及其他

如果文件存在则 PowerShell 的语法

如果文件存在,powershell 的语法如下:

Test-Path Cmdlet 语法:此 cmdlet 不仅用于检查文件是否存在,还用于检查路径是否存在。如果存在匹配则返回 true。如果路径为空,则返回错误。

NAME
Test-Path
SYNTAX
Test-Path [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType {Any | Container
| Leaf}] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan <datetime>]
[<CommonParameters>]
Test-Path -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-PathType {Any |
Container | Leaf}] [-IsValid] [-Credential <pscredential>] [-UseTransaction] [-OlderThan <datetime>] [-NewerThan
<datetime>]  [<CommonParameters>]
ALIASES
None
  • -Credential:此参数获取将用于执行此 cmdlet 的凭据。该参数的数据类型为PSCredentail。无是默认值。它可以处理管道输入,但不允许使用通配符。
  • -排除:这用于从搜索检查中排除某些内容。 String 是该参数的数据类型。 None 是默认值。它不处理管道输入,但接受通配符。
  • -过滤器:这用于对输入应用条件过滤。 String 是该参数的数据类型。 None 是默认值。它不处理管道输入,但接受通配符。
  • -包括:这包括需要检查是否存在的文件和路径。 String 是该参数的数据类型。 None 是默认值。它无法识别管道输入,并且接受通配符。
  • -IsValid:这检查路径或文件是否有效。如果语法有效,则返回 True,否则返回 false。 Switch 是该参数的数据类型。 None 是默认值。不接受管道输入,也不允许使用通配符。
  • -LiteralPath:这也像要测试的路径。唯一的区别是该参数的值被视为文字。 String 是该参数的数据类型。 Pspath 和 LP 是它的另一个别名。 None 是默认值。接受管道输入,但不允许使用通配符。
  • -NewerThan:这表示需要检查文件和路径的时间线更大。该参数的数据类型是datetime。 None 是默认值。无法识别管道输入,也不允许使用通配符。
  • -OlderThan:这表示时间线较旧,需要检查文件和路径。该参数的数据类型是datetime。 None 是默认值。无法识别管道输入,也不允许使用通配符。
  • -Pathtype:这表示正在搜索文件的路径类型。它可以接受容器、叶子和任何值作为值。容器的一个例子是目录;叶子的一个例子是一个文件,而任何一个都可以是两者之一。该参数的数据类型为TestPathType。别名是类型。 None 是默认值。它不接受管道输入,也不接受通配符。
  • 使用.Net方法:在dot net类中,有一个名为exists()的方法。这会检查文件是否存在,如果存在则返回 true。
  • 使用 Get-Item 和 Get-ChildItem:这用于检查一个或多个位置中的路径或文件。如果文件不存在,则会抛出错误。

语法:

NAME
Get-Item
SYNTAX
Get-Item [-Path] <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential
<pscredential>] [-UseTransaction] [-Stream <string[]>]  [<CommonParameters>]
Get-Item -LiteralPath <string[]> [-Filter <string>] [-Include <string[]>] [-Exclude <string[]>] [-Force] [-Credential
<pscredential>] [-UseTransaction] [-Stream <string[]>]  [<CommonParameters>]
ALIASES
gi
AME
Get-ChildItem
SYNTAX
Get-ChildItem [[-Path] <string[]>] [[-Filter] <string>] [-Include <string[]>] [-Exclude <string[]>] [-Recurse] [-Depth
<uint32>] [-Force] [-Name] [-UseTransaction] [-Attributes {ReadOnly | Hidden | System | Directory | Archive | Device |
Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted | IntegrityStream
| NoScrubData}] [-Directory] [-File] [-Hidden] [-ReadOnly] [-System]  [<CommonParameters>]
Get-ChildItem [[-Filter] <string>] -LiteralPath <string[]> [-Include <string[]>] [-Exclude <string[]>] [-Recurse]
[-Depth <uint32>] [-Force] [-Name] [-UseTransaction] [-Attributes {ReadOnly | Hidden | System | Directory | Archive |
Device | Normal | Temporary | SparseFile | ReparsePoint | Compressed | Offline | NotContentIndexed | Encrypted |
IntegrityStream | NoScrubData}] [-Directory] [-File] [-Hidden] [-ReadOnly] [-System]  [<CommonParameters>]
ALIASES
gci
ls
dir

如果文件存在则 PowerShell 示例

下面给出示例:

例子#1

输入:

Write-Host "Demo of if file exists" -ForegroundColor Green
$fi = 'c:\test.txt'
if (-not(Test-Path -Path $fi  -PathType Leaf)) {
try {
$null = New-Item -ItemType File -Path $fi  -Force -ErrorAction Stop
Write-Host "The file [$fi ] is created." -ForegroundColor Green
}
catch {
throw $_.Exception.Message
}
}
else {
Write-Host "Cannot create [$fi] because it is already available." -ForegroundColor Red
}

输出:

[玩转系统] 如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例

[玩转系统] 如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例

在上面的示例中,当第一次运行脚本时,该文件不存在,但它被创建了。由于文件已存在,因此第二次运行脚本时不会创建该脚本。

例子#2

输入:

write-host “使用获取项目和获取子项目进行文件检查的演示”-ForegroundColor 绿色

$fi = 'c:\test.txt'
Get-Item -Path $fi
Get-ChildItem -Path $fi
$fi1='c:\test1.txt'
Get-Item -Path $fi1
Get-ChildItem -Path $fi1
Write-Host "Demo of if file using net method" -ForegroundColor Green
[System.IO.File]::Exists($fi)
[System.IO.File]::Exists($fi1)

输出:

[玩转系统] 如果文件存在则使用 PowerShell |如果文件存在则 PowerShell 的语法和示例

结论

因此,本文详细介绍了检查 PowerShell 中文件是否存在的不同方法。它通过适当的示例解释了每个 cmdlet 的语法、其参数及其用法。要了解更多详细信息,最好编写示例脚本并执行它们。

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

取消回复欢迎 发表评论:

关灯