4.23. Creating and Deleting Shares

Problem

You want to create or delete a shared folder.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. In the left pane, browse to the folder you want to start or stop sharing.

  3. Right-click on the folder and select Sharing and Security (or Sharing on Windows 2000).

  4. To stop sharing the folder, select Do not share this folder.

  5. To share the folder, select Share this folder. Enter the Share name, enter a description for the share in the Comment field, and specify the User limit.

  6. Click OK to close the dialog box.

Using a command-line interface

The following command creates a share called Perl Libs:

> net share "Perl Libs"=d:\perl\lib /unlimited /remark:"Core Perl modules"

The /unlimited option means that an unlimited number of users can access the share simultaneously. You can limit the number of simultaneous users by using the /users: <Number> option instead.

This command deletes a share:

> net share "Perl Libs" /delete

Using VBScript

' This code creates a share.
' ------ SCRIPT CONFIGURATION ------
strComputer   = "."
strPath       = "d:\perl\lib"
strName       = "Perl Libs"
intType       = 0 ' share a disk drive resource
intMaxAllowed = 10 
strDescr      = "Core Perl modules"
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objShare = objWMI.Get("Win32_Share")
intRC = objShare.Create(strPath, strName, intType, intMaxAllowed, strDescr)
if intRC <> 0 then
   WScript.Echo "Error creating share: " & intRC
else
   WScript.Echo "Successfully created share"
end if
    
' This code deletes a share.
' ------ SCRIPT CONFIGURATION ------
strComputer   = "."
strName       = "Perl Libs"
' ------ END CONFIGURATION ---------
set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
set objShare = objWMI.Get("Win32_Share.Name='" & strName & "'")
intRC = objShare.Delete
if intRC <> 0 then
   WScript.Echo "Error deleted share: " & intRC
else
   WScript.Echo "Successfully deleted share"
end if

Discussion

After you create a share, you need to modify the access control list (ACL) to include the users and groups that can access the contents of the share (see Recipe 4.25 for more on this).

If you want to create a hidden share, simply append "$" to the end of the share name. The only difference in a hidden share is that it won't be directly viewable when listing the shared folders on a server. Hiding shares is kind of like hiding files (Recipe 4.12); it is up to the application to display them or not. So hidden shares are not truly hidden, but they will not be visible to the casual user.

See Also

MS KB 324267 (HOW TO: Share Files and Folders over the Network in a Windows Server 2003 Domain Environment)

Get Windows Server Cookbook now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.