Jump to content
Sign in to follow this  
potitpanda

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

Recommended Posts

potitpanda

I want to detect the presence of Shared folders, and delete them before running actions in my script

How can i do that

Anyone can give me an example ?

Thanks

Share this post


Link to post
Share on other sites
potitpanda

Sorry this is the bad section of the forum to post this topic

Anyone can delete this topic please ???

Thanks

Potitpanda

Share this post


Link to post
Share on other sites
rasim

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

Share this post


Link to post
Share on other sites
potitpanda

That's not OK...

I explain

I want to delete all the \\Server\Share on my local computer before activities of my script

I want to delete all of the Shared resources visible with Net use

Share this post


Link to post
Share on other sites
smashly

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

Share this post


Link to post
Share on other sites
rasim

smashly

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

Hi! Thank you for link. :)

Share this post


Link to post
Share on other sites
smashly

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

Share this post


Link to post
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
Sign in to follow this  

×