Search code examples
windowscommand-linepowershell-3.0nsis

How to get network path of a directory using command line


Say I have a directory called Temp in windows.

enter image description here

When I right click on it and open properties, and go to Sharing tab, I see this:

enter image description here

The field Network Path is Not Shared.

Now I click Share and select to share with and click ok.

The field Network Path now has value:

enter image description here

Is there a way to get the value of Network Path from command line.

Ideally I am looking for a command which takes directory path as input e.g. C:\Temp and output \\S5XXXXXXN\Temp if the directory is shared or Not Shared if the directory is not shared.

Background: I have a NSIS script to create an installer. The end of installation process is supposed to spit out a list of properties. One of the properties required for output is the Network Path of a directory. Inside NSIS what I have available is C:\Temp which I want to convert to \\S5XXXXXXN\Temp (if available) before spitting it out.

I tried to use this https://nsis.sourceforge.io/Get_Universal_Name but its not working.

DetailPrint "My directory is:"
# value of MYDIR is C:\Temp
DetailPrint $MYDIR
Push $MYDIR
Call get_universal_name
Pop $MYDIRNET
DetailPrint "My dir after call is: "
DetailPrint $MYDIRNET
# above also prints C:\Temp even though its shared and has value \\S5XXXXXXN\Temp

Update:

The answer below by @Anders works like a charm.

The list returned has an entry with blank $3 though. Just to be aware of and to add a necessary check if required.

enter image description here


Solution

  • get_universal_name calls WNetGetUniversalName and this function only supports mapped drive letters (net.exe use style). WNetGetConnection seems to have the same limitation. Even if this limitation did not exist it would still not work because a process elevated with UAC does not share the net connections with non-elevated processes.

    Going the other way does seem to work, getting a list of shares and their local paths:

    !include LogicLib.nsh
    
    Section "Check shares"
    System::Call 'NETAPI32::NetShareEnum(p0,i2,*p.r1,i-1,*i0r5,*i0,p0)i.r0'
    ${If} $0 = 0
        Push $1
        ${DoWhile} $5 <> 0
            System::Call '*$1(p.r2,i,p,i,i,i,p.r3,p,&l.r4)'
            IntPtrOp $1 $1 + $4
            System::Call '*$2(&w${NSIS_MAX_STRLEN}.r2)'
            System::Call '*$3(&w${NSIS_MAX_STRLEN}.r3)'
            System::Call 'NETAPI32::NetServerGetInfo(p0,i100,*p.r4)' ; Get computer name
            System::Call '*$4(i,w.s)'
            System::Call 'NETAPI32::NetApiBufferFree(p$4)'
            Pop $4
            DetailPrint "$3=\\$4\$2"
            ; ${If} $3 == "$MYDIR" ...TODO... ${EndIf}
            IntOp $5 $5 - 1
        ${Loop}
        System::Call 'NETAPI32::NetApiBufferFree(ps)'
    ${EndIf}
    SectionEnd