VBScripting: Working with Shortcuts in Windows (except WScript.Shell.Run())
In this article, we will explore the different ways to work with shortcuts in Windows using VBScript, excluding the use of the WScript.Shell.Run() method. We will cover key concepts related to creating, copying, and modifying shortcuts, providing detailed context and examples for each topic.
Creating Shortcuts with VBScript
To create a shortcut using VBScript, we can use the WScript.Shell object's CreateShortcut method. The following code snippet demonstrates how to create a shortcut to the Notepad application on the desktop:
Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("Desktop")
Set oLink = WshShell.CreateShortcut(strDesktop & "\Notepad.lnk")
oLink.TargetPath = "C:\Windows\System32
otepad.exe"
oLink.Save
Copying Shortcuts with VBScript
To copy a shortcut using VBScript, we can use the FileSystemObject's CopyFile method. The following code snippet demonstrates how to copy a shortcut from one location to another:
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\Users\Public\Desktop\Notepad.lnk", "C:\Users\User1\Desktop\Notepad.lnk"
Modifying Shortcut Properties with VBScript
To modify the properties of a shortcut using VBScript, we can use the WScript.Shell object's CreateShortcut method to load the shortcut, and then modify its properties as needed. The following code snippet demonstrates how to modify the icon of a shortcut:
Set WshShell = WScript.CreateObject("WScript.Shell")
Set oLink = WshShell.CreateShortcut("C:\Users\User1\Desktop\Notepad.lnk")
oLink.IconLocation = "C:\Windows\System32
otepad.exe, 0"
oLink.Save
Enumerate Shortcuts with VBScript
To enumerate all shortcuts in a directory using VBScript, we can use the FileSystemObject's GetFolder method to get a folder object, and then use the Files property to enumerate all files in the folder. We can then check if each file is a shortcut by looking at its Shortcut property. The following code snippet demonstrates how to enumerate all shortcuts in the desktop directory:
Set fso = CreateObject("Scripting.FileSystemObject")
Set objFolder = fso.GetFolder(WshShell.SpecialFolders("Desktop"))
For Each objFile In objFolder.Files
If objFile.Shortcut Then
WScript.Echo objFile.Name
End If
Next
In this article, we have covered key concepts related to working with shortcuts in Windows using VBScript, excluding the use of the WScript.Shell.Run() method. We have demonstrated how to create, copy, and modify shortcuts, as well as how to enumerate all shortcuts in a directory. With this knowledge, you can automate various tasks related to shortcuts in Windows using VBScript.
References
- Microsoft Docs: Create a Shortcut to an Item
- Microsoft Docs: Copy, Move, or Rename a File
- Microsoft Docs: Change a Shortcut's Properties
- Microsoft Docs: Enumerate Files and Folders