Create new absolute path from absolute path + relative or absolute path

╄→гoц情女王★ 提交于 2019-12-13 00:48:25

问题


I am working on a build script using psake and I need to create an absolute path from the current working directory with an inputted path which could either be a relative or absolute path.

Suppose the current location is C:\MyProject\Build

$outputDirectory = Get-Location | Join-Path -ChildPath ".\output"

Gives C:\MyProject\Build\.\output, which isn't terrible, but I would like without the .\. I can solve that issue by using Path.GetFullPath.

The problem arises when I want to be able to provide absolute paths

$outputDirectory = Get-Location | Join-Path -ChildPath "\output"

Gives C:\MyProject\Build\output, where I need C:\output instead.

$outputDirectory = Get-Location | Join-Path -ChildPath "F:\output"

Gives C:\MyProject\Build\F:\output, where I need F:\output instead.

I tried using Resolve-Path, but this always complains about the path not existing.

I'm assuming Join-Path is not the cmdlet to use, but I have not been able find any resources on how to do what I want. Is there a simple one-line to accomplish what I need?


回答1:


You could use GetFullPath(), but you would need to use a "hack" to make it use you current location as the current Directory(to resolve relative paths). Before using the fix, the .NET method's current directory is the working directory for the process, and not the location you have specified inside the PowerShell process. See Why don't .NET objects in PowerShell use the current directory?

#Hack to make .Net methods use the shells current directory instead of the working dir for the process
[System.Environment]::CurrentDirectory = (Get-Location)
".\output", "\output", "F:\output" | ForEach-Object {
    [System.IO.Path]::GetFullPath($_)
}

Output:

C:\Users\Frode\output
C:\output
F:\output

Something like this should work for you:

#Hack to make .Net methods use the shells current directory instead of the working dir for the process
[System.Environment]::CurrentDirectory = (Get-Location)

$outputDirectory = [System.IO.Path]::GetFullPath(".\output")



回答2:


I don't think there's a simple one-liner. But I assume you need the path created anyway, if it doesn't exist yet? So why not just test and create it?

cd C:\
$path = 'C:\Windows', 'C:\test1', '\Windows', '\test2', '.\Windows', '.\test3'

foreach ($p in $path) {
    if (Test-Path $p) {
        (Get-Item $p).FullName
    } else {
        (New-Item $p -ItemType Directory).FullName
    }
}


来源:https://stackoverflow.com/questions/29188848/create-new-absolute-path-from-absolute-path-relative-or-absolute-path

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!