通过 PowerShell 在 Chrome 中打开文件时的相对路径
Relative path when opening a file in Chrome via PowerShell
我在 Windows 10,我试图用 Chrome 打开一个文件,但是,PS 总是以相对路径结束。
我在名为 phy
的目录中执行 运行 命令,这是它的结构(相关部分):
.
├── defaults.json
└── docs
├── 11-01-physics-rotation-and-revolution.html
└── 11-09-physics-mechanical-properties-of-materials.html
我以多种方式尝试了以下每个命令。
- 我的 PATH
中没有 Chrome
- 重启后我的 PATH 上有 Chrome(具体来说,文件夹
C:\Program Files\Google\Chrome\Application
在我的用户 PATH 上)
- 使用
start chrome
代替 chrome
- 用
\
代替/
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
PS> Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
执行后,Chrome地址栏有./docs/11-09-physics-mechanical-properties-of-materials.html
(不是扩展版,字面意思是.
字符),Resolve-Path
,Convert-Path
,或者它是空白的,我得到新标签页。
以下命令按预期工作:
PS> chrome
PS> chrome google.com
PS> chrome D:\username\Documents\edu\College\attempt-2\Exams\JEE\Notes\self\phy\docs-09-physics-mechanical-properties-of-materials.html # this is the full path to the aforementioned phys directory
如何将相对路径转换为绝对路径?来自 Ubuntu,bash 几乎自动执行此操作。
要将相对路径转换为绝对路径,我使用了一个小辅助函数:
function ConvertTo-AbsolutePath ([string]$Path) {
if ($Path.StartsWith("~")) {
$Path = $Path.Substring(1)
$current = $HOME
}
else { $current = $pwd.ProviderPath }
if (-not ([System.IO.Path]::IsPathRooted($Path)) -or $Path -match '^\[^\]+') {
return [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($current, $Path))
}
$Path
}
用法:
$absPath = ConvertTo-AbsolutePath './docs/11-09-physics-mechanical-properties-of-materials.html'
然后用它作为参数开始chrome
您对 Convert-Path
should work properly and is the best way to handle relative paths, the only problem in this case is that you need to use the Grouping operator (..)
的调用使得 表达式 在我们传递给 chrome
之前先被计算:
chrome (Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html)
我在 Windows 10,我试图用 Chrome 打开一个文件,但是,PS 总是以相对路径结束。
我在名为 phy
的目录中执行 运行 命令,这是它的结构(相关部分):
.
├── defaults.json
└── docs
├── 11-01-physics-rotation-and-revolution.html
└── 11-09-physics-mechanical-properties-of-materials.html
我以多种方式尝试了以下每个命令。
- 我的 PATH 中没有 Chrome
- 重启后我的 PATH 上有 Chrome(具体来说,文件夹
C:\Program Files\Google\Chrome\Application
在我的用户 PATH 上) - 使用
start chrome
代替chrome
- 用
\
代替/
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> chrome Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html
PS> Resolve-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
PS> Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html | chrome
执行后,Chrome地址栏有./docs/11-09-physics-mechanical-properties-of-materials.html
(不是扩展版,字面意思是.
字符),Resolve-Path
,Convert-Path
,或者它是空白的,我得到新标签页。
以下命令按预期工作:
PS> chrome
PS> chrome google.com
PS> chrome D:\username\Documents\edu\College\attempt-2\Exams\JEE\Notes\self\phy\docs-09-physics-mechanical-properties-of-materials.html # this is the full path to the aforementioned phys directory
如何将相对路径转换为绝对路径?来自 Ubuntu,bash 几乎自动执行此操作。
要将相对路径转换为绝对路径,我使用了一个小辅助函数:
function ConvertTo-AbsolutePath ([string]$Path) {
if ($Path.StartsWith("~")) {
$Path = $Path.Substring(1)
$current = $HOME
}
else { $current = $pwd.ProviderPath }
if (-not ([System.IO.Path]::IsPathRooted($Path)) -or $Path -match '^\[^\]+') {
return [System.IO.Path]::GetFullPath([System.IO.Path]::Combine($current, $Path))
}
$Path
}
用法:
$absPath = ConvertTo-AbsolutePath './docs/11-09-physics-mechanical-properties-of-materials.html'
然后用它作为参数开始chrome
您对 Convert-Path
should work properly and is the best way to handle relative paths, the only problem in this case is that you need to use the Grouping operator (..)
的调用使得 表达式 在我们传递给 chrome
之前先被计算:
chrome (Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html)