Hey, Scripting Guy! How can I create a folder on the Start menu? I want the folder to be named the same as the %username% environment variable.
— SB
Hey, SB. As we are wont to do, we’re going to break this question down into subtasks. We’ll explain how to do each of these subtasks, then at the end put the pieces together to create a single script that solves your problem for you.
To begin with, we need to figure out the name of the logged-on user (which happens to map to the %username% environment variable). That’s easy: we just create an instance of the WSH Network object and retrieve the value of the UserName property. That’s what we do here:
Set objNetwork = CreateObject(“Wscript.Network”) strUser = objNetwork.UserName strPath = “C:\Documents and Settings\” & strUser
Note that we’ve also created a variable named strPath and assigned it the value C:\Documents and Settings\ plus the user name (e.g., kenmyer). That gives us a path to a user folder like C:\Documents and Settings\kenmyer. If you want to create a shortcut to a different folder (for example, \\atl-fs-01\public\kenmyer), you just need to change this line of code accordingly.
Next we need to determine the locations of the Start menu Programs folder. That’s also easy. This time we create an instance of the WSH Shell object and call the SpecialFolders method, telling SpecialFolders we’d like the location of the Programs folder. We store the folder path in the variable strPrograms:
Set objShell = CreateObject(“Wscript.Shell”) strPrograms = objShell.SpecialFolders(“Programs”)
(By the way, if you’d like to know the other folder paths you can locate using the SpecialFolders method, take a look at the “Working With Special Folders” section of the Microsoft Windows 2000 Scripting Guide.)
Finally we need to create the Start menu shortcut. Here’s the code that does that:
Set objShell = CreateObject(“Wscript.Shell”) Set objShellLink = objShell.CreateShortcut(strPrograms & “\” & strUser & “.lnk”) objShellLink.TargetPath = strPath objShellLink.Description = “Home folder for ” & strUser objShellLink.WorkingDirectory = strPath objShellLink.Save
Here we again use the WSH Shell object, this time calling the CreateShortcut method. To create the Start menu shortcut we need to pass CreateShortcut the complete path to the shortcut file we want to create (for example, C:\Documents and Settings\kenmyer\kenmyer.lnk). To determine this path, we simply combine the path to the user folder (C:\Documents and Settings\kenmyer), a slash (\), the user name (kenmyer), and the file extension for a shortcut file (.lnk). That’s what we do with this line of code:
Set objShellLink = objShell.CreateShortcut(strPrograms & “\” & strUser & “.lnk”)
Next we assign values to some of the shortcut file’s properties. The TargetPath represents the path to the folder; thus we assign it the path to the folder. The Description represents the tooltip that appears when you pause the mouse over the item in the Start menu; it gets a simple description indicating that it’s the home folder for the logged-on user. Finally, WorkingDirectory represents, well, the working directory for the shortcut; it gets assigned the path to the folder. After assigning these property values we call the Save method to save the shortcut file and add it to the Start menu.
Now, as promised, here’s a script that combines all three of our subtasks:
Set objNetwork = CreateObject(“Wscript.Network”) strUser = objNetwork.UserName strPath = “C:\Documents and Settings\” & strUserSet objShell = CreateObject(“Wscript.Shell”) strPrograms = objShell.SpecialFolders(“Programs”)
Set objShellLink = objShell.CreateShortcut(strPrograms & “\” & strUser & “.lnk”) objShellLink.TargetPath = strPath objShellLink.Description = “Home folder for ” & strUser objShellLink.WorkingDirectory = strPath objShellLink.Save
0 comments