ora*_*rad 100 powershell
我只是不喜欢以下语法:
if (Test-Path $path) { ... }
Run Code Online (Sandbox Code Playgroud)
和
if (-not (Test-Path $path)) { ... }
if (!(Test-Path $path)) { ... }
Run Code Online (Sandbox Code Playgroud)
特别是在检查"不存在"这种常见用途时,括号太多且不易读.有什么更好的方法呢?
更新:我目前的解决方案是使用别名exist,并not-exist为解释在这里.
PowerShell存储库中的相关问题:https://github.com/PowerShell/PowerShell/issues/1970
Mat*_*sen 109
如果您只想要替代cmdlet语法,特别是文件,请使用File.Exists().NET方法:
if(![System.IO.File]::Exists($path)){
# file with path $path doesn't exist
}
Run Code Online (Sandbox Code Playgroud)
另一方面,如果你想要一个通用的否定别名Test-Path,那么你应该怎么做:
# Gather command meta data from the original Cmdlet (in this case, Test-Path)
$TestPathCmd = Get-Command Test-Path
$TestPathCmdMetaData = New-Object System.Management.Automation.CommandMetadata $TestPathCmd
# Use the static ProxyCommand.GetParamBlock method to copy
# Test-Path's param block and CmdletBinding attribute
$Binding = [System.Management.Automation.ProxyCommand]::GetCmdletBindingAttribute($TestPathCmdMetaData)
$Params = [System.Management.Automation.ProxyCommand]::GetParamBlock($TestPathCmdMetaData)
# Create wrapper for the command that proxies the parameters to Test-Path
# using @PSBoundParameters, and negates any output with -not
$WrappedCommand = {
try { -not (Test-Path @PSBoundParameters) } catch { throw $_ }
}
# define your new function using the details above
$Function:notexists = '{0}param({1}) {2}' -f $Binding,$Params,$WrappedCommand
Run Code Online (Sandbox Code Playgroud)
notexists现在会表现得很像Test-Path,但总是会返回相反的结果:
PS C:\> Test-Path -Path "C:\Windows"
True
PS C:\> notexists -Path "C:\Windows"
False
PS C:\> notexists "C:\Windows" # positional parameter binding exactly like Test-Path
False
Run Code Online (Sandbox Code Playgroud)
正如你已经展示了自己,相反是很容易的,只是别名exists到Test-Path:
PS C:\> New-Alias exists Test-Path
PS C:\> exists -Path "C:\Windows"
True
Run Code Online (Sandbox Code Playgroud)
bri*_*ist 30
你发布的别名解决方案很聪明,但我会反对在脚本中使用它,因为我不喜欢在脚本中使用任何别名; 它往往会损害可读性.
如果这是您要添加到配置文件中的内容,那么您可以键入快速命令或将其用作shell,那么我可以看到它有意义.
您可能会考虑使用管道:
if ($path | Test-Path) { ... }
if (-not ($path | Test-Path)) { ... }
if (!($path | Test-Path)) { ... }
Run Code Online (Sandbox Code Playgroud)
或者,对于否定方法,如果适合您的代码,您可以将其作为肯定检查,然后else用于否定:
if (Test-Path $path) {
throw "File already exists."
} else {
# The thing you really wanted to do.
}
Run Code Online (Sandbox Code Playgroud)
小智 8
这是我的 PowerShell 新手的做法
if (Test-Path ".\Desktop\checkfile.txt") {
Write-Host "Yay"
}
else {
Write-Host "Damn it"
}
Run Code Online (Sandbox Code Playgroud)
添加以下别名.我认为这些应该默认在PowerShell中提供:
function not-exist { -not (Test-Path $args) }
Set-Alias !exist not-exist -Option "Constant, AllScope"
Set-Alias exist Test-Path -Option "Constant, AllScope"
Run Code Online (Sandbox Code Playgroud)
这样,条件语句将变为:
if (exist $path) { ... }
Run Code Online (Sandbox Code Playgroud)
和
if (not-exist $path)) { ... }
if (!exist $path)) { ... }
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
290826 次 |
| 最近记录: |