Powershell: resolve path that might not exist?

前端 未结 13 2466
再見小時候
再見小時候 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:46

    When Resolve-Path fails due to the file not existing, the fully resolved path is accessible from the thrown error object.

    You can use a function like the following to fix Resolve-Path and make it work like you expect.

    function Force-Resolve-Path {
        <#
        .SYNOPSIS
            Calls Resolve-Path but works for files that don't exist.
        .REMARKS
            From http://devhawk.net/blog/2010/1/22/fixing-powershells-busted-resolve-path-cmdlet
        #>
        param (
            [string] $FileName
        )
    
        $FileName = Resolve-Path $FileName -ErrorAction SilentlyContinue `
                                           -ErrorVariable _frperror
        if (-not($FileName)) {
            $FileName = $_frperror[0].TargetObject
        }
    
        return $FileName
    }
    
    0 讨论(0)
提交回复
热议问题