Thursday, March 4, 2010

VBScript: Enumerate Start Menu Shortcuts into XML

Here's a little script to dump a list of all shortcuts and their internal properties, which exist under the “All Users” profile, in Start Menu\Programs, as well as all sub-folders below that.  The output is basic raw XML.  Not much to it, but I had to crank this out for a special project I worked on a few weeks ago and thought I’d post it here.
'****************************************************************
' Filename..: enum_allusers_shortcuts.vbs
' Author....: skatterbrainz
' Date......: 02/03/2010
' Purpose...: enumerate shortcuts under all-users start menu (xml)
'****************************************************************
Dim objShell, allUsers, allStart, objFSO, oFolder, s

Set objShell = CreateObject("Wscript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

allUsers = objShell.ExpandEnvironmentStrings("%AllUsersProfile%")
allStart = allUsers & "\Start Menu\Programs"

Set oFolder = objFSO.GetFolder(allStart)

wscript.echo "<shortcuts>"
For each s in oFolder.SubFolders
ListShortcuts objFSO.GetFolder(allStart & "\" & s.Name)
Next
wscript.echo "</shortcuts>
"

Sub ListShortcuts(objFolder)
Dim objFile, filename
For each objFile in objFolder.Files
filename = objFile.Name
If Right(Lcase(filename),4) = ".lnk" Then
filepath = objFolder.Path & "\" & filename
wscript.echo vbTab & "<shortcut>"
ShortcutProps filepath
wscript.echo vbTab & "</shortcut>
"
End If
Next
End Sub

Sub ShortcutProps(scName)
Dim objShortcut
If objFSO.FileExists(scName) Then
Set objShortcut = objShell.CreateShortcut(scName)
WScript.Echo vbTab & vbTab & "<fullname>" & objShortcut.FullName & "</fullname>"
WScript.Echo vbTab & vbTab & "<arguments>" & objShortcut.Arguments & "</arguments>"
WScript.Echo vbTab & vbTab & "<workingpath>" & objShortcut.WorkingDirectory & "</workingpath>"
WScript.Echo vbTab & vbTab & "<target>" & objShortcut.TargetPath & "</target>"
WScript.Echo vbTab & vbTab & "<icon>" & objShortcut.IconLocation & "</icon>"
WScript.Echo vbTab & vbTab & "<hotkey>" & objShortcut.Hotkey & "</hotkey>"
WScript.Echo vbTab & vbTab & "<windowstyle>" & objShortcut.WindowStyle & "</windowstyle>"
WScript.Echo vbTab & vbTab & "<comment>" & objShortcut.Description & "</comment>"
Set objShortcut = Nothing
Else
wscript.echo "unable to find shortcut"
End If
End Sub

Set oFolder = Nothing
Set objFSO = Nothing
Set objShell = Nothing

No comments:

Post a Comment