11

In a blog post from Microsoft they illustrate how a URI can be written to specify local system file paths.

When sharing the path to network share files some chat programs will open these files in a browser.

So I hand code the changes needed to turn the windows path to a file URI

UNC Windows path: \\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt

becomes

URI: file://sharepoint.business.com/DavWWWRoot/rs/project%201/document.txt

I am tired of hand coding every time and was wondering if there was a way of quickly converting to a File URI.

I don't have admin permissions on my machine so I can't install software.

2
  • Are their any preferred tools/constraints? It could probably be done in something like Notepad++ with a few find/replaces, Excel, any general purpose programming language, possibly PowerShell scripting, etc
    – nijave
    Aug 23, 2016 at 16:05
  • no constraints, I just didn't know if there was a "Shift+Right-click" like shortcut Aug 23, 2016 at 17:00

4 Answers 4

5

PowerShell is an excellent way to automate tedious recurring tasks like the above!

Using PowerShell

Converting the above UNC path into a file URI is extremely simple using PowerShell (all versions), and requires only the format and replace operators, for example:

$Path = "\\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt"

# replace back slash characters with a forward slash, url-encode spaces,
# and then prepend "file:" to the resulting string

# note: the "\\" in the first use of the replace operator is an escaped
# (single) back slash, and resembles the leading "\\" in the UNC path
# by coincidence only

"file:{0}" -f ($Path -replace "\\", "/" -replace " ", "%20")

Which yields the following:

file://sharepoint.business.com/DavWWWRoot/rs/project%201/document.txt

As a Reusable Function

Finally, recurring tasks like the above should be made into PowerShell functions whenever possible. This saves time in the future, and ensures each task is always executed in exactly the same way.

The following function is an equivalent of the above:

function ConvertTo-FileUri {
    param (
        [Parameter(Mandatory)]
        [string]
        $Path
    )

    $SanitizedPath = $Path -replace "\\", "/" -replace " ", "%20"
    "file:{0}" -f $SanitizedPath
}

Once the function has been defined (and loaded into the current PowerShell session), simply call the function by name and provide the UNC path to convert as a parameter, for example:

ConvertTo-FileUri -Path "\\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt"
9

The simplest approach is to use the .Net URI class from your PowerShell code:

[System.Uri]'\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt' will give you a URI, and then the "AbsoluteURI" property will give you that as a string. So:

([System.Uri]'\\sharepoint.business.com\DavWWWRoot\rs\project 1\document.txt').AbsoluteUri

will give you what you want.

3

There is a simple and safe online converter doing this job: UNC Path to File URI Online Converter.

It is implemented with Javascript and the transformation is done completely in the browser so the path is not submitted to any server.

3

Combining what Andrew and Devyn came up with i have this function that accepts file paths that are relative as well such as ".\Documents\test.txt":

function ConvertTo-FileUri {
    param (
        [Parameter(Mandatory)]
        [string]
        $Path
    )
    ([system.uri](Get-Item $Path).FullName).AbsoluteUri
}
1
  • Thank you Brad, this is very helpful! Apr 23, 2020 at 14:06

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .