Jump to content

how to check and delete all the shared folders on a local computer


Recommended Posts

potitpanda

Hi! Try this:

$aShare = _NetGetShareList("")

If @error Then Exit

For $i = 1 To $aShare[0]
    ConsoleWrite("Deleting a " & $aShare[$i] & " share..." & @LF)
    RunWait(@ComSpec & " /c net share " & $aShare[$i] & " /delete", "", @SW_HIDE)
Next

Func _NetGetShareList($sComputer)
    Local $EntriesRead = DllStructCreate("int")
    Local $TotalEntries = DllStructCreate("int")
    Local $pBuffer = DllStructCreate("ptr"), $tBuffer
    
    $aRet = DllCall("netapi32.dll", "int", "NetShareEnum", _
        "wstr", $sComputer, _
        "dword", 0, _
        "ptr", DllStructGetPtr($pBuffer), _
        "dword", -1, _
        "ptr", DllStructGetPtr($EntriesRead), _
        "ptr", DllStructGetPtr($TotalEntries), _
        "ptr", 0)
    
    If $aRet[0] Then Return SetError(1, $aRet[0], 0)
    
    $EntriesRead = DllStructGetData($EntriesRead, 1)
    
    Local $aRes[$EntriesRead+1]=[$EntriesRead]
    Local $SHARE_INFO_0 = DllStructCreate("ptr["& $EntriesRead &"]", DllStructGetData($pBuffer, 1))
    
    For $i = 1 To $EntriesRead
        $tBuffer = DllStructCreate("wchar[256]", DllStructGetData($SHARE_INFO_0, 1, $i))
        $aRes[$i] = DllStructGetData($tBuffer, 1)
    Next
    DllCall("netapi32.dll", "int", "NetApiBufferFree", "ptr", DllStructGetData($pBuffer,1))
    Return $aRes
EndFunc
Edited by rasim
Link to comment
Share on other sites

Crude and maybe should add some sort of filter to the query so you only delete the specific shares you want..

This just deletes all shares on a local computer (well that's what it does my XP SP2 laptop).

$objWMIService = ObjGet("winmgmts:{impersonationLevel=impersonate}!\\" & @ComputerName & "\root\cimv2")
$colShares = $objWMIService.ExecQuery("Select * from Win32_Share")
For $objShare in $colShares
    $objShare.Delete   
Next

Cheers

Edit: I pieced the above crude example together from info from Microsoft Technet - Storage - Shared Folders

(just in case you were curious)

Edited by smashly
Link to comment
Share on other sites

Your welcome, I've been poking around there for the past couple of days.

I never played with autoit and objects, but the basics of it seems simple enough.

The biggest part is the error handling, it really bloats a simple 3 line script into 10 fold..lol

Cheers

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