Create Shortcuts w/ Windows Scripting Host

Windows uses two types of shortcuts. Shell Links which are binary files with .Lnk extensions and Internet Shortcuts or Favorites which are ASCII files with .Url extensions. I will show you how to create both types in this example. This, the simplest of my Create Shortcut programs, uses The Windows Scripting Host (WSH). Although this program is limited to discussing shortcuts, the WSH class contains some very useful methods, like those needed to perform registry operations. It may be worth while to reference the Windows Scripting Host Object Model in your project and press F2 to bring it up in the Object Browser.

Most likely you will want to create a shell link on your desktop to start the associated application. That is what this example does. However, by specifying a different special folder you can create the shortcut anywhere. See the code for more details on determining the location of Window's special folders.

The interesting point about internet shortcuts is that they use a URL which stands for Universal Resource Locator. As the name implies, the shortcut can point to just about anything, not just an internet address. You can enter the path to any file on your PC. The shortcut will then start the application associated with that file type and display the file. For example, if you enter "C:\Pictures\MyCat.jpg" the shortcut will start the application associated with .jpg files and display the image MyCat.jpg. (To see how to do this in code, see my ShellExecute page).

You can also wait for a process to terminate and read to/write from the registry using the Windows Scripting Host.

Create Shortcuts (shell links) with Windows Scripting Host
Download Source Code

Applies To

Wshom.ocx began shipping with Windows 98 but was available as an add on for Windows 95 and NT 4.0. You can download the Windows Script engine from Microsoft and it will work with Windows 9x, NT and Windows 2000. Visit the Scripting home page on Microsoft's site.

Form Code

To create a Shell Link (Shortcut):

   Private Sub cmdShellLink_Click()
      Dim sDesk As String
      Dim oShell As New IWshShell_Class
      Dim oShortCut As New IWshShortcut_Class

      sDesk = oShell.SpecialFolders.Item("Desktop")
      Set oShortCut = oShell.CreateShortcut(sDesk & "\" & Trim$(txtName) & ".lnk")

      With oShortCut
         .TargetPath = Trim$(txtTarget)
         .Description = txtDescription
         .Arguments = txtArguments
         .WorkingDirectory = txtStartIn
         .WindowStyle = cboStyles.ItemData(cboStyles.ListIndex)
         .Save
      End With

      Set oShell = Nothing
      Set oShortCut = Nothing
   End Sub

To create an Internet Shortcut:

   Private Sub cmdURL_Click()
      Dim sDesk As String
      Dim lFNum As Long
      Dim oShell As New IWshShell_Class

      sDesk = oShell.SpecialFolders.Item("Desktop")
      lFNum = FreeFile
      Open sDesk & "\" & Trim$(txtURLName) & ".url" For Output As lFNum
      Print#lFNum, "[InternetShortcut]"
      Print #lFNum, "URL=" & Trim$(txtURL)
      Close #lFNum
      Set oShell = Nothing
   End Sub

Batch Files

Here's the cool part. You can enter this code into a file with a .VBS extension and double click on it. Your code will run as long as you have Wscript.exe installed. For more details on creating .VBS files see my wait for a process to terminate and read to/write from the registry page.

Instructions

Download the source code and press F5 to run it. Enter the information for the type of shortcuts you want to create and click the Create button. That's it.




About TheScarms
About TheScarms


Sample code
version info

If you use this code, please mention "www.TheScarms.com"

Email this page


© Copyright 2024 TheScarms
Goto top of page