Jump to content

Find sharename using path


xinx
 Share

Recommended Posts

Hi all,

I am trying to find a way that will allow me to get the sharename of a folder if I already know it's local or UNC path. SO far the code I have will show whether the folder is shared or not but does not display what the sharname is.

Here's a snippet of the existing code:

------------------------------------------------------------------------

#include <WinAPIEx.au3>

$sFolder = $ProfileShare

$tSHFILEINFO = DllStructCreate($tagSHFILEINFO)

_WinAPI_ShellGetFileInfo($sFolder, $SHGFI_ATTRIBUTES, 0, $tSHFILEINFO)

$iAttributes = DllStructGetData($tSHFILEINFO, "Attributes")

If BitAND($iAttributes, $SFGAO_SHARE) Then

MsgBox (1, "Share info", "Shared")

Else

MsgBox (1, "Share info", "Not shared")

EndIf

------------------------------------------------------------------------

($ProfileShare is basically elswhere in my main program but simply points to C:\Test\Sharedfolder)

What I want is to output the sharename of Sharedfolder (currently with a sharename of Shared) in the first message box - is this at all possible?

Many thanks in advance! :mellow:

Link to comment
Share on other sites

That's a tough one. I didn't find anything native in AutoIt that can do it. From what I can tell, you would have to look at this registry section: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\LanmanServer\Shares. From there, you would have to make a list of the shares on the computer, decode the paths from hex to strings and then compare your path to the ones from the share list to find a match. It's too late for me to attempt this tonight. Sorry.

#include <ByteMe.au3>

Link to comment
Share on other sites

That's a tough one. I didn't find anything native in AutoIt that can do it. From what I can tell, you would have to look at this registry section: HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\LanmanServer\Shares. From there, you would have to make a list of the shares on the computer, decode the paths from hex to strings and then compare your path to the ones from the share list to find a match. It's too late for me to attempt this tonight. Sorry.

Many thanks for your reply - at least I've now got an idea of what to do haha but yeah I think it's a bit beyond my capabilities with AutoIT :-/

Link to comment
Share on other sites

Many thanks for your reply - at least I've now got an idea of what to do haha but yeah I think it's a bit beyond my capabilities with AutoIT :-/

And if you can work this one out then I will be very thankful and also regard you as a pure genius! :mellow:

Link to comment
Share on other sites

On my Windows 7 x86 machine, I have a network drive which is the Z: drive. In the Registry is the "HKCU\Network\Z" key, there's a value called RemotePath, and in that value the string is a plain-text showing the exact path to the share drive. So, all you would need to do is get the list of your drive letters and determine which are network shares rather than connected drives, and look for the drive letter in that key.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

On my Windows 7 x86 machine, I have a network drive which is the Z: drive. In the Registry is the "HKCU\Network\Z" key, there's a value called RemotePath, and in that value the string is a plain-text showing the exact path to the share drive. So, all you would need to do is get the list of your drive letters and determine which are network shares rather than connected drives, and look for the drive letter in that key.

Hi BrewManH,

Many thanks for your advice, it is greatly appreciated :-) Sadly I'm working on Server 2008, and so there's no shared folders showing in HKCU\Network. I can see the sharenames of folders in HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\LanmanServer\Shares as sleepydvdr suggested but do now have the know-how to do the rest :-(

Link to comment
Share on other sites

The key for the share is located between the Path= and stops where Permissions= starts, get the key using RegRead, then use _StringBetween to find what the shared folder/drive path is.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I'm quite new to AutoIT but from what I understand using RegRead will only work if I already know what the sharename is? So when I look in the registry I can see what the path name is but

Here's what I am trying to do:

On a server there are folders that may already be shared, for example g:\users\profiles is shared but we don't know what it is shared as (well we do, but we want this script to work that out)

This has to work in such a way that when the script is run it will look for the path g:\users\profiles, remove it's existing sharename and add a new sharename.

The problem I have is that when the folder already has an existing sharename and I use Net_Share_ShareAdd it will only add the possible sharename but not remove the old one.

I found that I cannot use Net_Share_ShareDel unless I know what the sharename is. And I'm trying to do this for multiple folders :-/ which makes automation difficult...

Any help greatly appreciatedcd :mellow:

Link to comment
Share on other sites

Look at RegEnumKey and RegEnumVal to get all the keys in the Shares registry setting and loop through it to find all of your shares. Look at the examples in the help file as to how to do that.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

Right, so far this is what I've found:

#include <array.au3>
#include <File.au3>
;*****************************************************
; Function _RegSearch($sStartKey, $sSearchVal, $iType = 0x07, $fArray = False)
;    Where:  $sStartKey = Reg path at which to begin search
;            $sSearchVal = The string to search for
;            $iType = Matching types to return:
;                1 = Key names
;                2 = Value names
;                4 = Value data
;                Add bits together for multiple match types, default is 7 (all)
;            $fArray = Return an array of results vice the string (defualt = False)
;    Performs a recursive search of the registry starting at $sStartKey, looking for $sSearchVal
;    Returns a string containing a list of key names and values.
;   If a key name matches, it is listed as a reg path with trailing backslash:
;    i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\
;   If a value name matches, it is listed as a reg path without trailing backslash:
;    i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir
;   If the data matches, the format is path = data:
;      i.e. HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WallPaperDir = %SystemRoot%\Web\Wallpaper
;    If $fArray is True, then return data is an array with [0] = count.
;*****************************************************
; Change Log:
;  v1.0.0.0  |  03/17/05  |  Original SearchReg() by Holger
;  v2.0.0.0  |  08/10/06  |  Native AutoIt version by PsaltyDS
;  v2.0.0.1  |  08/16/06  |  Fixed bug reported by markloman
;  v2.0.1.0  |  07/30/08  |  Added $iType and $fArray parameters
;  v2.0.2.0  |  11/12/08  |  Fixed bug returning array [0] = 1 vice 0 for none found
;  v2.0.3.0  |  06/22/10  |  Fixed bug appending some result strings together reported by squid808
;*****************************************************
Func _RegSearch($sStartKey, $sSearchVal, $iType = 0x07, $fArray = False)
    Local $v, $sVal, $k, $sKey, $sFound = "", $sFoundSub = "", $avFound[1] = [0]

    ; Trim trailing backslash, if present
    If StringRight($sStartKey, 1) = "\" Then $sStartKey = StringTrimRight($sStartKey, 1)

    ; Generate type flags
    If Not BitAND($iType, 0x07) Then Return SetError(1, 0, 0); No returns selected
    Local $fKeys = BitAND($iType, 0x1), $fValue = BitAND($iType, 0x2), $fData = BitAND($iType, 0x4)

    ; This checks values and data in the current key
    If ($fValue Or $fData) Then
        $v = 1
        While 1
            $sVal = RegEnumVal($sStartKey, $v)
            If @error = 0 Then
                ; Valid value - test its name
                If $fValue And StringInStr($sVal, $sSearchVal) Then $sFound &= $sStartKey & "\" & $sVal & @LF

                ; test its data
                If $fData Then
                    $readval = RegRead($sStartKey, $sVal)
                    If StringInStr($readval, $sSearchVal) Then
                        $sFound &= $sStartKey & "\" & $sVal & " = " & $readval & @LF
                    EndIf
                EndIf
                $v += 1
            Else
                ; No more values here
                ExitLoop
            EndIf
        WEnd
    EndIf

    ; This loop checks subkeys
    $k = 1
    While 1
        $sKey = RegEnumKey($sStartKey, $k)
        If @error = 0 Then
            ; Valid key - test it's name
            If $fKeys And StringInStr($sKey, $sSearchVal) Then $sFound &= $sStartKey & "\" & $sKey & "\" & @LF

            ; Now search it
            $sFoundSub = _RegSearch($sStartKey & "\" & $sKey, $sSearchVal, $iType, False) ; use string mode to test sub keys
            If $sFoundSub <> "" Then $sFound &= $sFoundSub & @LF
        Else
            ; No more keys here
            ExitLoop
        EndIf
        $k += 1
    WEnd

    ; Return results
    If StringRight($sFound, 1) = @LF Then $sFound = StringTrimRight($sFound, 1)
    If $fArray Then
        If StringStripWS($sFound, 8) <> "" Then $avFound = StringSplit($sFound, @LF)
        Return $avFound
    Else
        Return $sFound
    EndIf
EndFunc   ;==>_RegSearch

$hkeyloc="HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\LanmanServer\Shares"
$sharepath="Path"

$results=_regsearch($hkeyloc, $sharepath, 7, true)

;_arrayDisplay($results) ; commented out, used for checking only

$sSearch = "G:\students\profiles"
If @error Then Exit

$iIndex = _ArraySearch($results, $sSearch, 0, 0, 0, 1)

If $iIndex = -1 Then
    MsgBox (16, "Error", "No such path exists")
Else
    MsgBox (64, "Result", $iIndex)
EndIf

So now what I am getting is the MsgBox displaying the value of $iIndex which for me shows as 127 - I check this against the _arrayDisplay($results) and indeed it is the correct path, however the sharename is two rows down in some instances and in others it is three rows after $iIndex (depending on if there are share comments). I've only put in the message box for testing purposes but once it displays the correct output then I know this program is working.

Link to comment
Share on other sites

  • 3 weeks later...

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...