从绝对路径+相对或绝对路径创建新的绝对路径
Create new absolute path from absolute path + relative or absolute path
我正在使用 psake 编写构建脚本,我需要使用输入的路径从当前工作目录创建一个绝对路径,该路径可以是相对路径也可以是绝对路径。
假设当前位置是C:\MyProject\Build
$outputDirectory = Get-Location | Join-Path -ChildPath ".\output"
给出 C:\MyProject\Build\.\output
,这并不可怕,但我希望没有 .\
。我可以使用 Path.GetFullPath
.
来解决这个问题
当我希望能够提供绝对路径时,问题就出现了
$outputDirectory = Get-Location | Join-Path -ChildPath "\output"
给出 C:\MyProject\Build\output
,而我需要 C:\output
。
$outputDirectory = Get-Location | Join-Path -ChildPath "F:\output"
给出 C:\MyProject\Build\F:\output
,而我需要 F:\output
。
我尝试使用 Resolve-Path
,但这总是抱怨路径不存在。
我假设 Join-Path
不是要使用的 cmdlet,但我无法找到有关如何执行所需操作的任何资源。有没有简单的一行就可以完成我所需要的?
您可以使用 GetFullPath()
,但您需要使用 "hack" 才能将您的当前位置用作当前目录(以解析相对路径)。在使用修复之前,.NET 方法的当前目录是进程的工作目录,而不是您在 PowerShell 进程中指定的位置。参见 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($_)
}
输出:
C:\Users\Frode\output
C:\output
F:\output
像这样的东西应该适合你:
#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")
我认为没有简单的单行代码。但是我假设您无论如何都需要创建路径,如果它不存在的话?那么,为什么不直接测试并创建它呢?
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
}
}
我正在使用 psake 编写构建脚本,我需要使用输入的路径从当前工作目录创建一个绝对路径,该路径可以是相对路径也可以是绝对路径。
假设当前位置是C:\MyProject\Build
$outputDirectory = Get-Location | Join-Path -ChildPath ".\output"
给出 C:\MyProject\Build\.\output
,这并不可怕,但我希望没有 .\
。我可以使用 Path.GetFullPath
.
当我希望能够提供绝对路径时,问题就出现了
$outputDirectory = Get-Location | Join-Path -ChildPath "\output"
给出 C:\MyProject\Build\output
,而我需要 C:\output
。
$outputDirectory = Get-Location | Join-Path -ChildPath "F:\output"
给出 C:\MyProject\Build\F:\output
,而我需要 F:\output
。
我尝试使用 Resolve-Path
,但这总是抱怨路径不存在。
我假设 Join-Path
不是要使用的 cmdlet,但我无法找到有关如何执行所需操作的任何资源。有没有简单的一行就可以完成我所需要的?
您可以使用 GetFullPath()
,但您需要使用 "hack" 才能将您的当前位置用作当前目录(以解析相对路径)。在使用修复之前,.NET 方法的当前目录是进程的工作目录,而不是您在 PowerShell 进程中指定的位置。参见 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($_)
}
输出:
C:\Users\Frode\output
C:\output
F:\output
像这样的东西应该适合你:
#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")
我认为没有简单的单行代码。但是我假设您无论如何都需要创建路径,如果它不存在的话?那么,为什么不直接测试并创建它呢?
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
}
}