Powershell: resolve path that might not exist?

前端 未结 13 2483
再見小時候
再見小時候 2020-12-02 13:05

I\'m trying to process a list of files that may or may not be up to date and may or may not yet exist. In doing so, I need to resolve the full path of an item, even though

13条回答
  •  悲&欢浪女
    2020-12-02 13:28

    I ended up with this code in my case. I needed to create a file later in the the script, so this code presumes you have write access to the target folder.

    $File = ".\newdir\newfile.txt"
    If (Test-Path $File) {
        $Resolved = (Resolve-Path $File).Path
    } else {
        New-Item $File -ItemType File | Out-Null
        $Resolved = (Resolve-Path $File).Path
        Remove-Item $File
    }
    

    I also enclosed New-Item in try..catch block, but that goes out of this question.

提交回复
热议问题