Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Script to convert UNC to URL

548 views
Skip to first unread message

Foss

unread,
Oct 25, 2004, 6:37:01 AM10/25/04
to
Hi there all,

I've got a number of users who publish data to the intranet by copying it
onto a server then making links to it.

I'd like to make a script for the people who are finding it a little
confusing to convert the UNC of the file into a URL.

Here's an example:
UNC: \\web1a\PIBReports\BCA\Admin_Reports\User_List.pdf
URL: http://web1/pibreports/bca/admin_reports/user_list.pdf

I realise this is a pretty basic one but I'm still having trouble with it!

Any help much appreciated.

Cheers,
Pete
--
Cheers,
Foss

Joe Fawcett

unread,
Oct 25, 2004, 7:35:49 AM10/25/04
to
"Foss" <Fo...@discussions.microsoft.com> wrote in message
news:0871B4DC-4FFF-4FBE...@microsoft.com...
What you want is a reverse Server.MapPath, it's not at all basic. I gave up
after trying many methods including reading the IIS metabase.
The best I could manage was that if you know the documents are always going
to pibreports and you know what the URL for that folder is you strip off
anything after pibreports by using InStr and Mid and then replace all the
backslashes with forward ones. You can then add this back to your known
base:

Function GetUrlFromUnc(Unc)
Const BASE_URL = "http://web1/"
Const BASE_FOLDER = "pibreports"
Dim iPos
iPos = InStr(1, Unc, BASE_FOLDER, vbTextCompare)
If iPos > 0 Then
Dim sSubPath
sSubPath = Mid(Unc, iPos)
sSubPath = Replace(sSubPath, "\", "/")
GetUrlFromUnc = BASE_URL & sSubPath
Else
'Invalid Folder
GetUrlFromUnc = ""
End If
End Function

Dim sInitialUnc
sInitialUnc = "\\web1\PIBReports\BCA\Admin_Reports\User_List.pdf"
WScript.echo GetUrlFromUnc(sInitialUnc)

--
Joe (MVP)


McKirahan

unread,
Oct 25, 2004, 7:41:30 AM10/25/04
to
"Foss" <Fo...@discussions.microsoft.com> wrote in message
news:0871B4DC-4FFF-4FBE...@microsoft.com...

Is the URL's "web1" supposed to be "web1a" like the UNC?

UNC: \\web1a\PIBReports\BCA\Admin_Reports\User_List.pdf
URL: http://web1/pibreports/bca/admin_reports/user_list.pdf

If so then perhaps you want:

URL = "http:" & Replace(UNC,"\","/")

This also presumes that case sensitibvity is not an issue.


Foss

unread,
Oct 25, 2004, 7:55:01 AM10/25/04
to
Hi there Joe,

That's perfect! Thank you very much.. Thankfully, PIBReports is a share so
that'll always be there and your script works perfectly!

Cheers,
Pete

Torgeir Bakken (MVP)

unread,
Oct 25, 2004, 8:03:25 AM10/25/04
to
Foss wrote:

> Hi there Joe,
>
> That's perfect! Thank you very much.. Thankfully, PIBReports is a
> share so that'll always be there and your script works perfectly!

Hi

You might want to use the undocumented Escape function to url
encode the path (e.g. a space will be converted to %20):

'--------------------8<----------------------
sUNC = "\\web1a\PIBReports\BCA\Admin_Reports\User_List.pdf"

' Replace \ with /
sTmp = Replace(sUNC, "\", "/")

' url encode sTmp using the undocumented Escape function
sURL = "http:" & Escape(sTmp)

MsgBox sURL
'--------------------8<----------------------

--
torgeir, Microsoft MVP Scripting and WMI, Porsgrunn Norway
Administration scripting examples and an ONLINE version of
the 1328 page Scripting Guide:
http://www.microsoft.com/technet/scriptcenter/default.mspx

Foss

unread,
Oct 25, 2004, 8:07:02 AM10/25/04
to
Hi there,

The UNC and URLs I wrote are correct I'm afraid.
There are three servers web1a, b and c. These, I think, are all used to feed
the http://web1 intranet home page. This makes it a little more confusing but
not too bad!

Thanks very much, that's going to be useful for something else I'm doing
later as well!

Cheers,
Pete

Foss

unread,
Oct 25, 2004, 8:39:05 AM10/25/04
to
Wow, I'm getting lots of tips now. Very handy indeed!

Here's the completed mixture of suggestions in case it's of interest:

'-----------------------------------


Function GetUrlFromUnc(Unc)
Const BASE_URL = "http://web1/"
Const BASE_FOLDER = "pibreports"
Dim iPos
iPos = InStr(1, Unc, BASE_FOLDER, vbTextCompare)
If iPos > 0 Then
Dim sSubPath
sSubPath = Mid(Unc, iPos)
sSubPath = Replace(sSubPath, "\", "/")

sSubPath = Escape(sSubPath)


GetUrlFromUnc = BASE_URL & sSubPath
Else
'Invalid Folder
GetUrlFromUnc = ""
End If
End Function

strCopy = GetUrlFromUnc(InputBox("Please enter the file path"))

Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy
objIE.Quit
msgbox("The HyperLink (URL) to your file has been copied to the clipboard,
please use the Paste command on the Edit menu or 'CTRL+V' to use it." +
Chr(10) + strCopy)
'-----------------------------------

Al Dunbar [MS-MVP]

unread,
Oct 25, 2004, 9:55:01 PM10/25/04
to

"McKirahan" <Ne...@McKirahan.com> wrote in message
news:KD5fd.311717$D%.292605@attbi_s51...

URL = "http:" & lcase( Replace(UNC,"\","/") )

/Al


Foss

unread,
Oct 26, 2004, 4:21:02 AM10/26/04
to
Cheers Al, much appreciated! It now looks like this:

'-------------------------------------------------------------
on error resume next

Function GetUrlFromUnc(Unc)
Const BASE_URL = "http://web1/"
Const BASE_FOLDER = "pibreports"
Dim iPos
iPos = InStr(1, Unc, BASE_FOLDER, vbTextCompare)
If iPos > 0 Then
Dim sSubPath
sSubPath = Mid(Unc, iPos)
sSubPath = Replace(sSubPath, "\", "/")
sSubPath = Escape(sSubPath)

GetUrlFromUnc = lcase(BASE_URL & sSubPath)


Else
'Invalid Folder
GetUrlFromUnc = ""
End If
End Function

AppCap = "Convert File Path to HyperLink"


strCopy = GetUrlFromUnc(InputBox("Please enter the file

path",AppCap,"\\web1a\PIBReports\"))

If strCopy <> "" Then


Set objIE = CreateObject("InternetExplorer.Application")
objIE.Navigate("about:blank")
objIE.document.parentwindow.clipboardData.SetData "text", strCopy
objIE.Quit

MsgBox "The HyperLink (URL) to your file has been copied to the clipboard,

please use the Paste command on the Edit menu or 'CTRL+V' to use it." +

Chr(10) + strCopy, 64, AppCap
Else
End If
'-------------------------------------------------------------

0 new messages