Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Resolve-Path with -Relative parameter should allow the use of a reference path #8435

Closed
terrabitz opened this issue Dec 10, 2018 · 7 comments · Fixed by #19358
Closed
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-Fixed The issue is fixed. WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Comments

@terrabitz
Copy link

Summary of the new feature/enhancement

As a PowerShell developer, I want to be able to obtain the path of an item relative to a path that is not my current directory so that I can avoid changing my current location context for a relative directory lookup.

Proposed technical implementation details

Assuming I have a directory like this:

/MyProject
    |-/dir
        |-test_1.txt
        |-test_2.txt
        |-/subdir
            |-test_3.txt

Currently, if I want to do a resolve a path relative to a different directory, I have to use set-location before calling Resolve-Path. For example, if I wanted to get all my .txt file locations relative to dir, I would have to do something like this:

/MyProject> cd dir
/MyProject/dir> Get-ChildItem -Recurse -Include "*.txt" | Resolve-Path -Relative
./subdir/test_3.txt
./test_1.txt
./test_2.txt
/MyProject/dir> cd ..

Ideally, I would like to do something like this:

/MyProject> Get-ChildItem .\dir -Recurse -Include "*.txt" | Resolve-Path -Relative -ReferencePath .\dir
./subdir/test_3.txt
./test_1.txt
./test_2.txt
@iSazonov iSazonov added Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc. labels Dec 10, 2018
@Jaykul
Copy link
Contributor

Jaykul commented Feb 5, 2019

Totally agree. We shouldn't have to Push-Location just to use this feature.

@rkeithhill
Copy link
Collaborator

You could use Join-Path -Resolve for this:

PS> Join-Path $home\Downloads ../Documents -Resolve
C:\Users\Keith\Documents

This also has the benefit of correcting the directory sep char for the platform.

@terrabitz
Copy link
Author

@rkeithhill I don't think that'll work for this particular problem. It appears that the -Resolve switch on Join-Path is only for checking whether the resulting joined path actually exists. At least on the surface, it doesn't appear to help with relative resolution. From the docs:

-Resolve
Indicates that this cmdlet should attempt to resolve the joined path from the current provider.

* If wildcards are used, the cmdlet returns all paths that match the joined path.
* If no wildcards are used, the cmdlet will error if the path does not exist.

@an-dr-eas-k
Copy link

an-dr-eas-k commented Sep 4, 2019

This is my workaround:

function Resolve-RelativePath {
	[CmdletBinding()]
	param(
		[Parameter(Mandatory, ValueFromPipeline)]
		[string[]]$Path,
		[string]$ReferencePath = "."
	)
	begin{
		$ReferencePath = (Get-Item $ReferencePath).FullName
	}
	process {
		foreach ($pItem in $Path){
			$pItem = (Get-Item $pItem).FullName
			[System.IO.Path]::GetRelativePath($ReferencePath, $pItem)
		}
	}
}

@chriskuech
Copy link

This would help mitigate #10509 . Would love to see some traction on this, as it seems pretty simple.

@iSazonov
Copy link
Collaborator

What if we add new parameter for base path Resolve-Path -BasePath as optional for Relative parameter?

@ghost ghost added the In-PR Indicates that a PR is out for the issue label Mar 16, 2023
@ghost ghost added Resolution-Fixed The issue is fixed. and removed In-PR Indicates that a PR is out for the issue labels Mar 22, 2023
@ghost
Copy link

ghost commented Apr 20, 2023

🎉This issue was addressed in #19358, which has now been successfully released as v7.4.0-preview.3.:tada:

Handy links:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Discussion the issue may not have a clear classification yet. The issue may generate an RFC or may be reclassif Resolution-Fixed The issue is fixed. WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module WG-Engine-Providers built-in PowerShell providers such as FileSystem, Certificates, Registry, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants