Is there any straight-forward way (by use of cmdlets or .NET classes) to obtain only the relative path of a file in a sub-folder from a given path?
eg current folder is C:\MyScript
and there is a sub-folder called Data
with a file Test.txt
, so I would like to see Data\Test.txt
instead of C:\MyScript\Data\Test.txt
path. relpath() method in Python is used to get a relative filepath to the given path either from the current working directory or from the given directory. Note: This method only computes the relative path.
Click the Start button and then click Computer, click to open the location of the desired file, hold down the Shift key and right-click the file. Copy As Path: Click this option to paste the full file path into a document. Properties: Click this option to immediately view the full file path (location).
The Resolve-Path
cmdlet has a -Relative
parameter that will return a path relative to the current directory:
Set-Location C:\MyScript $relativePath = Get-Item Data\Test.txt | Resolve-Path -Relative
In case the directory doesn't exist, or the base directory should be different from the current working directory, there's a .NET method [IO.Path]::GetRelativePath(String from, String to)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With