How Can I Create a Shortcut in My Network Places?

How Can I Create a Shortcut in My Network Places?

  • Comments 5
  • Likes
Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I create a shortcut in My Network Places?

-- KP

SpacerHey, Scripting Guy! AnswerScript Center

Hey, KP. You’ll have to excuse us if we sound a little out of breath; we’ve been running around like crazy looking for a complicated and highly-technical solution to this problem. It was only after we failed miserably in that quest that we began to think about what we were trying to do. Maybe we were trying to make this much harder than we needed to. After all, My Network Places is nothing more than a folder on the hard disk, and a “network place” is nothing more than a shortcut. Could creating a network place involve nothing more than creating a shortcut in a folder? You be the judge:

Const NETHOOD = &H13&

Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path

strShortcutName = "Finance Department Public Folder"
strShortcutPath = "\\atl-finance-01\public"

Set objShortcut = objWSHShell.CreateShortcut _
    (strNetHood & "\" & strShortcutName & ".lnk")
objShortcut.TargetPath = strShortcutPath
objShortcut.Save

We begin by defining a constant named NETHOOD and assigning it the value &H13&; we’ll use this in a moment to locate the My Network Places folder. We then create instances of two difference objects: the Windows Script Host Shell object and the Windows Shell object. That’s what these two lines of code do:

Set objWSHShell = CreateObject("Wscript.Shell")
Set objShell = CreateObject("Shell.Application")

Next we use a Windows Shell object method and property - Namespace and Self, respectively - to determine the location of the My Network Places folder. The Namespace method actually locates the folder for us; however, it returns a Folder object. For technical reasons we don’t need to worry about, we can’t do much with this object; therefore we use the Self property to return a FolderItem object representing the My Network Places folder. We do that because a FolderItem object is something that our script can deal with.

As soon as we have a FolderItem we can then grab the Path property and assign it to the variable strNetHood; strNetHood will now be equal to something along the lines of C:\Documents and Settings\kenmyer\NetHood. And, yes, that is a long and convoluted explanation for just three lines of code:

Set objFolder = objShell.Namespace(NETHOOD)
Set objFolderItem = objFolder.Self
strNetHood = objFolderItem.Path

Next we simply assign the name of the shortcut and the path to our network place to a pair of variables. We don’t really need to do this; we could just hard-code the values into the script. But assigning the values to variables will make it a little easier for you to modify the script to fit your own needs.

Now we turn to the WSH Shell object to actually create our shortcut. We begin by calling the CreateShortcut method, passing it the path to the new shortcut name. That will be a combination of four items: the path to the My Network Places folder (strNetHood), a trailing \, the name of our new shortcut (strShortcutName), and the file extension .lnk. That gives us a shortcut path similar to this:

C:\Documents and Settings\kenmyer\NetHood\Finance Department Public Folder.lnk

We set the TargetPath property of the shortcut to the network place (\\atl-finance-01\public) and then call the Save method to write the new shortcut to the file system. Just like that, we have a new network place.

Now, if you’ll excuse us, we need to go lie down for a moment. Who’d have thought that scripting would be such hard work?

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Thanks for the script. I have changed it sightly. So that the objects in My Network Places are mapped according to which OU in AD the user is located and to what Shared Folder objects are published.

    The issue i have is that on a Windows 2000 SP4 the script runs very quickly however on Windows XP SP2 it takes about 4 minutes. Any ideas? WSH upgrade to 5.7 made no difference.

    Regards

    Const NETHOOD = &H13&

    'Create objects

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set WshNetwork = CreateObject("WScript.Network")

    Set objSysInfo = CreateObject("ADSystemInfo")

    Set objWSHShell = CreateObject("Wscript.Shell")

    Set objShell = CreateObject("Shell.Application")

    Set objFolder = objShell.Namespace(NETHOOD)

    Set objFolderItem = objFolder.Self

    strNetHood = objFolderItem.Path

    On Error Resume Next

    strPath = objFolderItem.Path & "\*.*"

    On Error Resume Next

    objFSO.DeleteFile strPath, true

    'Some shortcuts are folders, loop and delete all of them too

    Set oFolder = objFSO.GetFolder(objFolderItem.Path)

    Set oSubFolders= oFolder.SubFolders

    For Each oCurrentSubFolder in oSubFolders

    On Error Resume Next

    objFSO.DeleteFolder oCurrentSubFolder, true

    Next

    If Instr(objSysInfo.username,",") <> 0 Then

    UserOU=Right(objSysInfo.Username,Len(objSysInfo.Username)-Instr(objSysInfo.Username,","))

    End If

    'Enumerate Shares in the user OU.

    Set objOU = GetObject("LDAP://" & UserOU)

    objOU.filter = Array("Volume")

    For Each objitem In objOU

    'Add the Published Shares in the USer OU to MY Network Places

    Set objShortcut = objWSHShell.CreateShortcut _    (strNetHood & "\" & objitem.cn & ".lnk")

    objShortcut.TargetPath = objitem.uncname

    objshortcut.Description = objitem.uncname

    objShortcut.Save

    next

  • A "Network Place" is NOT a shortcut in the NetHood folder!  There aren't a lot of people in the world who know this, so we probably need to address that, first.  A "Network Place" is a folder on a remote machine that is integrated into the local Windows namespace, much like a symbolic link.  It's called a "Folder Shortcut", though admittedly that name doesn't clarify much.

    Run the VB script.  Note the icon it creates is different from the rest.  There's a reason for that.  Now open the Folder (treeview) pane in Explorer and expand the My Network Places branch.  All other Places (created automatically or via Network Place wizard) are nodes on the My Network Places branch, and can be expanded to reveal their subfolders.  But the shortcut created by the script does NOT appear in the My Network Places node because it is only a Shortcut...it is NOT incorporated into Windows' namespace any more than any other shortcut.

    So this is not a script for creating a Folder Shortcut...it's a script for creating an ordinary Shortcut in a folder.

    To see what a Network Place REALLY looks like, open a CMD window, CD to the NetHood folder, and type ATTRIB -R [networkplacename].  You'll see it's really a "normal" folder containing a normal shortcut named TARGET.LNK, and a DESKTOP.INI file.  When the read-only flag is raised, Windows inserts the target of the shortcut into the Windows namespace under its parent, in this case My Network Places.

    It can appear elsewhere...the Desktop, drive C:, in %TEMP%, wherever you put the Folder Shortcut is where Windows will splice it in to the namespace.

    (BTW, Windows also creates a Folder Shortcut if if you drag a Folder and drop it on the Start menu.)

    CAUTION: It's critical that you don't accidentally create a "circular" namespace with this technique.  It's very easy to do by mistake.  Explorer will hang very quickly as you browse it.  For example, if you have a Folder Shortcut on your Desktop that points to \\SomeServer\SomeShare, and in \\SomeServer\SomeShare you have another that points to your My Documents folder, you have created a circular namespace.  So don't!

    I script Folder Shortcut creation with a batch file that copies the (non-read-only) folder, desktop.ini and target.lnk to the destination (in my case, the All Users Desktop) and then uses ATTRIB to set the read-only bit.

    IF EXIST "%ALLUSERSPROFILE%\Desktop\MyNetPlace" GOTO END

    xcopy %0\..\"MyNetPlace" "%ALLUSERSPROFILE%\Desktop\MyNetPlace" /I /S /Y

    Attrib +R "%ALLUSERSPROFILE%\Desktop\MyNetPlace"

    :END

    Contents of the folder are the target.lnk as described above, and the desktop.ini which follows:

    [.ShellClassInfo]

    CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}

    Flags=2

    Desktop.ini's are normally super-hidden files (ATTRIB +S +H), but if this one has normal attributes it doesn't hurt anything.  The read-only attribute of its parent folder is all that matters.

    If you use this batch file, it will work correctly if the Folder Shortcut does not already exist.  If it already exists, however, and you use the script hoping to change it to point to some other target (by, for example, replacing the first line with RD /S /Q), the new Folder Shortcut won't work correctly.  When you browse the Network Place, the address in the Address Bar is the folder that contains the Folder Shortcut, not the Target, although it does display the target's contents.  So far, only way I've found to change the folder shortcut programatically is to delete it, reboot, then create the new one.  True for Win XP SP3, anyway.

    Came here hoping for a better way, but that was not to be.  But at least you now have a down-and-dirty way to create a true Folder Shortcut.

    BTW, the technique works in Windows 2000 through Vista/WS2008, but not Win7/WS2008R2.  The Folder Shortcut is created, but does NOT join the Windows namespace.  Only way I've found to approximate this in Win 6.1 RTM is to use Symbolic Links, which is unfortunate because their paths are relative to the containing folder instead of the remote share.  Hoping it's a bug that will be fixed in 6.1 SP1, and that it's not "by design".

  • Thank you, JRV, for describing the proper way to do this.

    Here is a VBScript version of JRV's batch file.  I've tested it on Windows XP and it works fine.

    Sub AddMyNetworkPlace(strPath)

    Dim objFSO, objShell, objMNP, objFolder, objFile, objLNK, strName

    strName = Mid(strpath,InStrRev(strpath,"\") +1)

    Set objFSO = CreateObject("Scripting.FileSystemobject")

    Set objShell = CreateObject("WScript.Shell")

    Set objMNP = objFSO.GetFolder(objShell.SpecialFolders("Nethood"))

    Set objFolder = objMNP.SubFolders.Add(strName)

    objFolder.Attributes = 1

    Set objFile = objFSO.CreateTextFile(objFolder.Path & "\desktop.ini")

    With objFile

    .WriteLine "[.ShellClassInfo]"

    .WriteLine "CLSID2={0AFACED1-E828-11D1-9187-B532F1E9575D}"

    .WriteLine "Flags=2"

    .Close

    End With

    Set objFile = objFSO.GetFile(objFolder.Path & "\desktop.ini")

    objFile.Attributes = 6

    Set objLNK = objShell.CreateShortcut(objFolder.Path & "\target.lnk")

    With objLNK

    .TargetPath = strPath

    .Save

    End With

    Set objLNK = Nothing

    Set objFile = Nothing

    Set objFolder = Nothing

    Set objMNP = Nothing

    Set objShell = Nothing

    Set objFSO = Nothing

    End Sub

  • How to I remove these shortcuts within a script?

  • Is there an easy way to add this script as a function in HTML and have it run using the onclick for a button?