Export (0) Print
Expand All

Mapping Shared Folders to Local Folders

Microsoft® Windows® 2000 Scripting Guide

Shared folders have properties - such as the maximum allowable connections - that are available through the Win32_Share class. It is easy to forget, however, that in addition to being virtual entities available over the network, shared folders are actual physical file folders on a computer hard disk. As physical file folders, shared folders have additional properties, such as the date the folder was last modified and whether or not the folder is compressed or encrypted, that are not available using the Win32_Share class.

If you need to retrieve the actual folder properties of a shared folder, you can use the Win32_ShareToDirectory class. Win32_ShareToDirectory is an association class that associates a shared folder with an actual file folder; you simply pass Win32_ShareToDirectory the name of a shared folder, and it returns a reference to the actual physical file folder. After you have that reference, you can retrieve all the properties available through the Win32_Directory class.

Scripting Steps

Listing 11.34 contains a script that maps a shared folder named Scripts to a local folder. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use an Associators of query to return a collection of local folders mapped to shared folders. To ensure that only the local folder that corresponds to the shared folder Scripts is returned, a clause is included limiting data retrieval to folders that have the share name Scripts.

  4. For the single folder in the collection, echo the folder Name. This is not the share name, but the folder name as retrieved using the Win32_Directory class.

Listing 11.34 Mapping a Shared Folder to a Local Folder

  
1
2
3
4
5
6
7
8
9


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery _
 ("ASSOCIATORS OF {Win32_Share.Name='Scripts'} WHERE " _
 & "AssocClass=Win32_ShareToDirectory")
For Each objFolder in colShares
 Wscript.Echo objFolder.Name
Next

You can also create a script that maps all the shared folders on a computer to their local counterparts. This requires you to do two things:

  • Return a collection of all the shared folders on a computer.

  • For each shared folder in that collection, use an Associators of query to determine the local folder.

Listing 11.35 contains a script that maps all the shared folders on a computer to their local equivalents. To carry out this task, the script must perform the following steps:

  1. Create a variable to specify the computer name.

  2. Use a GetObject call to connect to the WMI namespace root\cimv2, and set the impersonation level to "impersonate."

  3. Use an ExecQuery call to return a collection of all the shared folders on the computer.

  4. For each shared folder in the collection, use an Associators of query to return a collection consisting of the local folder mapped to the shared folder.

    To determine the shared folder to be mapped, you use the share names returned by the Win32_Share class. For example, suppose three shared folders are returned:

    • Share1

    • Share2

    • Share3

    The first time the For Each loop is run, the name Share1 is passed to the Associators of query. On the next iteration, the name Share2 is passed to the query. This continues until each of the shared folder names has been used in the query.

  5. For the single local folder in the new collection, echo the share name and the folder name.

Listing 11.35 Mapping All Shared Folders to Local Folders

  
1
2
3
4
5
6
7
8
9
10
11
12
13


strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colShares = objWMIService.ExecQuery _
 ("SELECT * FROM Win32_Share")
For Each objShare in colShares
 Set colAssociations = objWMIService.ExecQuery _
 ("ASSOCIATORS OF {Win32_Share.Name='" & objShare.Name & "'} " _
 & " WHERE AssocClass=Win32_ShareToDirectory")
 For Each objFolder in colAssociations
 Wscript.Echo objShare.Name & vbTab & objFolder.Name
 Next
Next

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft