I'm on Windows 10, and I'm trying to open a file with Chrome, however, PS always ends up sending it the relative path.
I'm running commands inside a directory called phy
, and here is it's structure (relevant parts):
.
├── defaults.json
└── docs
├── 11-01-physics-rotation-and-revolution.html
└── 11-09-physics-mechanical-properties-of-materials.html
I tried each of the following commands in several ways.
- without Chrome on my PATH
- with Chrome on my PATH after reboot (specifically, the folder
C:\Program Files\Google\Chrome\Application
is on my user PATH) - using
start chrome
instead ofchrome
- with
\
instead of/
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
After executing, the address bar of Chrome has either ./docs/11-09-physics-mechanical-properties-of-materials.html
(not the expanded version, it literally gets the .
character), Resolve-Path
, Convert-Path
, or it's blank and I get the New Tab page.
The following commands work as expected:
PS> chrome
PS> chrome google.com
PS> chrome D:\username\Documents\edu\College\attempt-2\Exams\JEE\Notes\self\phy\docs\11-09-physics-mechanical-properties-of-materials.html # this is the full path to the aforementioned phys directory
How do I get it to convert a relative path to an absolute one? Coming from Ubuntu, bash does this pretty much automatically.
CodePudding user response:
To convert a relative path to absolute, I use a small helper function:
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
}
Usage:
$absPath = ConvertTo-AbsolutePath './docs/11-09-physics-mechanical-properties-of-materials.html'
Then use that as parameter to start chrome
CodePudding user response:
Your call to 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 (..)
so that the expression is evaluated first before we pass it to chrome
:
chrome (Convert-Path ./docs/11-09-physics-mechanical-properties-of-materials.html)