A better way to check if a path exists or not in PowerShell

前端 未结 7 2144
名媛妹妹
名媛妹妹 2020-11-29 20:32

I just don\'t like the syntax of:

if (Test-Path $path) { ... }

and

if (-not (Test-Path $path)) { ... }
if (!(Test-Path $pa         


        
7条回答
  •  遥遥无期
    2020-11-29 21:31

    To check if a Path exists to a directory, use this one:

    $pathToDirectory = "c:\program files\blahblah\"
    if (![System.IO.Directory]::Exists($pathToDirectory))
    {
     mkdir $path1
    }
    

    To check if a Path to a file exists use what @Mathias suggested:

    [System.IO.File]::Exists($pathToAFile)
    

提交回复
热议问题