Jump to content

UNC Path Name


pdaughe
 Share

Recommended Posts

Hello,

A while back I posted a question regarding how to obtain the UNC path name for a given path. A forum member (jinxter) replied and gave me a solution (thanks again, jinxter, for your help).

I post my implementation of jinster's solution here for others, although I realize there may be a better way...

Sincerely,

Paul

AutoItSetOption ('MustDeclareVars', 1)      ;Explictly declare all variables

Local $Path = @DocumentsCommonDir & "\" & "Shared Music"
MsgBox (0, "Get UNC Path", "Path = " & $Path & @CRLF & @CRLF & "UNC = " & Get_UNC_Path($Path) )

#include-once
;===============================================================================
;
; Function Name:    Get UNC Path()
; Description:      Get the UNC path for a given local path (folder or file)
; Parameter(s):     $Path [required] - path on local computer
; Requirement(s):   None
; Return Value(s):  On Success: UNC path 
;                   On Failure: null string
;                      @error = 0 - no UNC path exists for $Path
;                      @error = 1 - error accessing registry
;                         @extended = 1 - unable to enumerate share names
;                         @extended = 2 - unable to read share name value
; Author(s):        Paul Daughenbaugh (thanks to jinxter)
;
;===============================================================================
Func Get_UNC_Path ($Path)

Local Const $RegKey = "HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares"

Local $I, $J, _
      $Share_Name, $Share_Value, _
      $Keywords, $Next_Keyword

For $I = 1 to 100
    $Share_Name = RegEnumVal ($RegKey, $I)
    If @error <> 0 Then
       If @error = -1 Then Return ""
       Return SetError (1, 1, "")
    EndIf
    $Share_Value = RegRead ($RegKey, $Share_Name)
    If @error Then Return SetError (1, 2, "")
    $Keywords = StringSplit ($Share_Value, @CRLF)
    For $J = 1 to $Keywords[0]
        $Next_Keyword = StringSplit ($Keywords[$J], "=")
        If $Next_Keyword[0] < 2 Then ContinueLoop
        If $Next_Keyword[1] = "Path" Then
           If StringLeft ($Path, StringLen ($Next_Keyword[2])) = $Next_Keyword[2] Then _
              Return "\\" & @ComputerName & "\" & $Share_Name & StringTrimLeft ($Path, StringLen ($Next_Keyword[2]))
        EndIf
    Next
Next

Return ""

EndFunc
Edited by pdaughe
Link to comment
Share on other sites

Here is the script I use. Assuming it is what you were looking for. Your script looks like it is pulling local paths.

I made this one just to get the beginning path \\[server]\[volume] of a mapped drive letter. I don't know if it works on local windows drives.

$NETDIR=@ScriptDir; Gets the path of this script when it is run.

;Convert the drive letter to UNC if not UNC already, then remove everything but the \\[server]\[volume].
if DriveMapGet(StringLeft($NETDIR,2)) Then
    $aNetdir=DriveMapGet(StringLeft($NETDIR,2));Convert drive letter to UNC
Else
    $getslash=StringInStr($NETDIR,"\",0,4);Already UNC so get the location of the fourth \
    $aNetdir=StringLeft($NETDIR,$getslash-1);Save everything before the fourth \
EndIf
$NETDIR=$aNetdir

Be open minded but not gullible.A hammer sees everything as a nail ... so don't be A tool ... be many tools.

Link to comment
Share on other sites

Paul,

Great function. I am sure I will be using it in the near future. FWIW, here is a Win XP batch file that can do the same thing.

-John

@echo off
for /f "usebackq tokens=1,2,3,4 delims= " %%i in (`net use^|findstr /i %~d1`) do @echo "%%k%~p1%~n1%~x1"

Example:

J:\scripts\misc>truename.bat test.au3

"\\myserver\users\john\scripts\misc\test.au3"

Link to comment
Share on other sites

Jftuga, that Dos command is wild -- I used to know DOS quite well, but it's been a while....

I am creating a folder on Computer A that contains shortcuts to files on computer A and other computers in the home network.

The folder that contains the shortcuts is itself shared. In order for the shortcuts to be valid when accessed by a different computer, the shortcut targets must be UNC path names.

The script in the first post provided a solution, BUT -- I didn't get very far. I quickly discovered that Computer A could not update the targets of the shortcuts (i.e. the files) when using the UNC path name. That is because the shortcut folder is "read-only" share and even though the files (shortcut targets) themselves are not read-protected and in fact reside on Computer A, Computer A is denied access when the files are accessed via the UNC path name (Windows doesn't resolve it).

So, the script below is the opposite. In my case, Computer A calls the script right after the $Shortcut_Details = FileGetShortcut ("lnk"), passing the script $Shortcut_Details[0]. Computer A can then update the files on Computer A using the local path (with several people in the house, I didn't want to make the share read/write, which really would have been a band-aid solution anyway. Those kind of solutions always end-up getting you!)

#include-once
;===============================================================================
;
; Function Name:    Get Local Path()
; Description:      Get the local path for a given UNC path on the host computer
; Parameter(s):     $UNC_Path [required] - UNC path on host computer
; Requirement(s):   None
; Return Value(s):  On Success: local path 
;                   On Failure: null string
;                      @error = 0 - no local path on host computer exists for $UNC_Path
;                      @error = 1 - error accessing registry
;                         @extended = 1 - unable to enumerate share names
;                         @extended = 2 - unable to read share name value
; Author(s):        Forum members
;
;===============================================================================
Func Get_Local_Path ($Path)

Local Const $RegKey = "HKLM\SYSTEM\CurrentControlSet\Services\lanmanserver\Shares"

Local $I, $J, _
      $Share_Name, $Share_Value, _
      $Keywords, $Next_Keyword, _
      $UNC_Path

For $I = 1 to 100
    $Share_Name = RegEnumVal ($RegKey, $I)
    If @error <> 0 Then
       If @error = -1 Then Return ""
       Return SetError (1, 1, "")
    EndIf
    $Share_Value = RegRead ($RegKey, $Share_Name)
    If @error Then Return SetError (1, 2, "")
    $Keywords = StringSplit ($Share_Value, @CRLF)
    For $J = 1 to $Keywords[0]
        $Next_Keyword = StringSplit ($Keywords[$J], "=")
        If $Next_Keyword[0] < 2 Then ContinueLoop
        If $Next_Keyword[1] = "Path" Then
           $UNC_Path = "\\" & @ComputerName & "\" & $Share_Name
           If StringLeft ($Path, StringLen ($UNC_Path)) = $UNC_Path Then _
              Return $Next_Keyword[2] & StringTrimLeft ($Path, StringLen ($UNC_Path))
        EndIf
    Next
Next

Return ""

EndFunc
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...