Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Powershell: resolve path that might not exist?

Tags:

powershell

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 the item may be specified with relative paths. However, Resolve-Path prints and error when used with a non-existant file.

For example, What's the simplest, cleanest way to resolve ".\newdir\newfile.txt" to "C:\Current\Working\Directory\newdir\newfile.txt" in Powershell?

Note that System.IO.Path's static method use with the process's working directory - which isn't the powershell current location.

like image 874
Eamon Nerbonne Avatar asked Jun 14 '10 15:06

Eamon Nerbonne


People also ask

What is LiteralPath in PowerShell?

-LiteralPath. Specifies the path to be resolved. The value of the LiteralPath parameter is used exactly as typed. No characters are interpreted as wildcard characters. If the path includes escape characters, enclose it in single quotation marks ( ' ).

How do I get the absolute path of a file in PowerShell?

Use the Get-ChildItem cmdlet in PowerShell to get the full path of the file in the current directory. Get-ChildItem returns one or more items from the specified location and using the file FullName property, it gets the full path of the file.

How do you resolve a path?

The path. resolve() method is used to resolve a sequence of path-segments to an absolute path. It works by processing the sequence of paths from right to left, prepending each of the paths until the absolute path is created. The resulting path is normalized and trailing slashes are removed as required.


2 Answers

You want:

c:\path\exists\> $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath(".\nonexist\foo.txt") 

returns:

c:\path\exists\nonexists\foo.txt 

This has the advantage of working with PSPaths, not native filesystem paths. A PSPath may not map 1-1 to a filesystem path, for example if you mount a psdrive with a multi-letter drive name.

What's a pspath?

ps c:\> new-psdrive temp filesystem c:\temp ... ps c:\> cd temp: ps temp:\>  

temp:\ is a drive-qualified pspath that maps to a win32 (native) path of c:\temp.

-Oisin

like image 168
x0n Avatar answered Oct 11 '22 11:10

x0n


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 } 
like image 29
joshuapoehls Avatar answered Oct 11 '22 10:10

joshuapoehls