Mar 08 2013

Retrieve the shortcut properties from all the shortcuts in the Start Menu

image_pdfimage_print

As a packager it is needed to retrieve the shortcut properties. What executable is used in each shortcut.

On MS Forums I started a threat regarding this one. This is the final result:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME: RetrieveAllShortCutProperties_v006.vbs
'
' AUTHOR: Willem-Jan
' DATE  : 04-09-2009
'
' COMMENT: 
'  This script is used to inventory all the shortcuts in a given folder and
'  its subfolders.
'
' Version-history:
'  v.0.0.1:
'   Initial version
'
'  v 0.0.2:
'   Also output to a Word Document
'
'  v.0.0.3:
'   Creates only a Word Document. 
'
'  v.0.0.4:
'   Also makes a print screen from a shortcut.
'   Does only work on XP ENG
'
'  v.0.0.5:
'   Correction Tom Lavedas added.
' 
'  v.0.0.6:
'   Improved Windows 7 support
'
'==========================================================================
 
Option Explicit
 
'==========================================================================
'Declare the variables
'==========================================================================
 
Dim objApplication
Dim objFolder
Dim objStartFolder
Dim objFSO
Dim objFile
dim objShell
Dim objFolderItem
dim objApplicationLink
Dim objWord
Dim objExplorer
Dim objWordBasic
Dim strWordOutputFile
dim objOutputFile
Dim subFolder
dim strFolder
Dim colFiles
Dim cntLNKFiles
Dim strShortcut
Dim objRange
Dim objTable
Dim objDoc
Dim objSelection
dim cntTables
Dim sLnkPath
Dim sLnkName
Dim strAction
Dim strArguments
Dim strLogFile
Dim strEntry
Dim oFile
 
Dim arrShortcut
Dim strShortcutName
Dim strShortcutFolder
 
Dim strReadyMessage
 
 
'==========================================================================
'Set the variables
'==========================================================================
 
Set objFSO            = CreateObject("Scripting.FileSystemObject")
Set objApplication    = CreateObject("Shell.Application")
Set objShell          = CreateObject("WScript.Shell")
Set objWord           = CreateObject("Word.Application")
Set objWordBasic      = CreateObject("Word.Basic")
Set objExplorer       = createobject("internetexplorer.application")
cntLNKFiles           = 0
cntTables             = 1
 
 
Const END_OF_STORY          = 6
Const MOVE_SELECTION        = 0
Const LOGGING               = True
Const wdLineStyleDot        = 2
Const wdLineStyleDashDotDot = 6
Const wdLineStyleDashDot    = 5
Const wdLineStyleNone       = 0
Const wdBorderBottom        = -3
Const wdBorderHorizontal    = -5
Const wdBorderLeft          = -2
Const wdBorderRight         = -4
Const wdBorderTop           = -1
Const wdBorderVertical      = -6
Const wdBorderDiagonalDown  = -7
Const wdBorderDiagonalUp    = -8
 
strLogFile = "C:\TEMP\Discover_shortcuts.log"
 
 
Set objFolder         = objApplication.BrowseForFolder (0, "Select the folder to search for .lnk files:", (0))
If objFolder Is Nothing Then
   Wscript.Quit
     Else
   Set objFolderItem = objFolder.Self
   objStartFolder    = objFolderItem.Path
End If
 
 
'==========================================================================
'If the selected folder does not end with a '\' add one.
'==========================================================================
 
If Right(objStartFolder,1)<>"\" Then
   objStartFolder = objStartFolder & "\"
End If
 
 
'==========================================================================
'Define the outfputfile
'==========================================================================
 
strWordOutputFile = objStartFolder & "_resultsfile_LNK_files.doc"
 
'==========================================================================
'Delete a previous version (if exits)
'==========================================================================
 
If objFSO.FileExists(strWordOutputFile) Then
   objFSO.DeleteFile(strWordOutputFile)
end If
 
'==========================================================================
'Create C:\TEMP (if not exists)
'==========================================================================
 
If Not objFSO.FolderExists("C:\TEMP") Then
       objFSO.CreateFolder("C:\TEMP")
end If
 
'==========================================================================
'Delete a previous logfile (if exits)
'==========================================================================
If LOGGING Then
  If objFSO.FileExists(strLogFile) Then
     objFSO.DeleteFile(strLogFile)
  end If
  Set objOutputFile              = objFSO.CreateTextFile (strLogFile)
End If
 
'==========================================================================
'Create the Word document
'==========================================================================
 
objWord.Visible      = False
Set objDoc           = objWord.Documents.Add()
Set objSelection     = objWord.Selection
If LOGGING Then
   WriteToLogFile("Word document generated in the background")
End If
 
'==========================================================================================================
'Al dan niet tonen van mededelingen op het scherm.
'==========================================================================================================
 
objExplorer.Navigate "about:blank"   
objExplorer.ToolBar                       = 0
objExplorer.StatusBar                     = 0
objExplorer.Width                         = 600
objExplorer.Height                        = 200
objExplorer.Left      = 100
objExplorer.Top       =  100
Do While (objExplorer.Busy)
    Wscript.Sleep 200
Loop    
objExplorer.Visible                       = 1
 
'==========================================================================
'Look in the root of the given foldername.
'==========================================================================
 
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    if lcase(right(objFile,3)) = "lnk" Then
       cntLNKFiles = cntLNKFiles + 1
       If LOGGING Then
          WriteToLogFile("LNK file found and processing: " & objFile)
       End If
       Readshortcut(objFile)
    end if
Next
 
'==========================================================================
'And call the subfolders.
'==========================================================================
 
ShowSubFolders objFSO.GetFolder(objStartFolder)
 
objDoc.SaveAs(strWordOutputFile)
objWord.Quit
objExplorer.Quit
 
 
strReadyMessage = "The script has finished." & vbCrlf & "See the resultsfile (doc): " & strWordOutputFile & "." & vbcrlf
strReadyMessage = strReadyMessage & "There are " & cntLNKFiles & " shortcuts found in the folder " & objStartFolder & " and its subfolders."
If LOGGING then
   strReadyMessage = strReadyMessage & vbcrlf & "The logfile is: " & strLogFile & "."
end if
 
wscript.echo strReadyMessage
 
'==========================================================================================================
'Procedure: Readshortcut(strShortcut)
'Target: request shortcut properties
'Usage:
'   Readshortcut(strShortcut) where strShortcut is the lnk file name.
'==========================================================================================================
 
Sub Readshortcut(strShortcut)
 
 
 set objApplicationLink = objShell.CreateShortcut(strShortcut)   ' Execute the createshortcut method, which also retrieves an existing shortcut
 
 ShowProgress("Processing file: " & strShortcut)
 
 arrShortcut       = split(strShortcut,"\")
 strShortcutName   = arrShortcut(ubound(arrShortcut))
 strShortcutFolder = left(strShortcut,len(strShortcut)-len(strShortcutName))
 
 If LOGGING Then
    WriteToLogFile("     Value 'strShortcutName':   " & strShortcutName)
    WriteToLogFile("     Value 'strShortcutFolder': " & strShortcutFolder)
 End If
 
  objSelection.TypeParagraph()
 Set objRange = objSelection.Range
 objDoc.Tables.Add objRange, 7, 2
 Set objTable = objDoc.Tables(cntTables)
 objWord.Selection.Style = "Tabelraster"
 
With objDoc.Tables(cntTables)
   .Borders(wdBorderLeft).LineStyle         = wdLineStyleDot
   .Borders(wdBorderRight).LineStyle        = wdLineStyleDot
   .Borders(wdBorderTop).LineStyle          = wdLineStyleDot
   .Borders(wdBorderBottom).LineStyle       = wdLineStyleDot
   .Borders(wdBorderVertical).LineStyle     = wdLineStyleDot
   .Borders(wdBorderHorizontal).LineStyle   = wdLineStyleDot
   .Borders(wdBorderDiagonalDown).LineStyle = wdLineStyleNone
   .Borders(wdBorderDiagonalUp).LineStyle   = wdLineStyleNone
   .Borders.Shadow = False
End With
 
 
 objTable.Cell(1, 2).Select
 Call getSCPropImage (objFile)
 
 
 objTable.Cell(2, 1).Range.Text = "Shortcutname:"
 objTable.Cell(2, 2).Range.Text = strShortcut
 
 objTable.Cell(3, 1).Range.Text = "Targetpath:"
 objTable.Cell(3, 2).Range.Text = objApplicationLink.TargetPath & " " & objApplicationLink.Arguments
 
 objTable.Cell(4, 1).Range.Text = "Working directory:"
 objTable.Cell(4, 2).Range.Text = objApplicationLink.WorkingDirectory
 
 objTable.Cell(5, 1).Range.Text = "Hotkey:"
 objTable.Cell(5, 2).Range.Text = objApplicationLink.Hotkey
 
 objTable.Cell(6, 1).Range.Text = "Description:"
 objTable.Cell(6, 2).Range.Text = objApplicationLink.Description
 
 objTable.Cell(7, 1).Range.Text = "Icon location:"
 objTable.Cell(7, 2).Range.Text = objApplicationLink.IconLocation
 
 objTable.Columns.Item(1).Width = objWord.InchesToPoints(2)
 objTable.Columns.Item(2).Width = objWord.InchesToPoints(5)
 
 objSelection.EndKey END_OF_STORY
 objSelection.TypeParagraph()
 
 cntTables = cntTables + 1
 
End Sub
 
'==========================================================================================================
'Procedure: ShowSubFolders(strFolder)
'Target: Search for lnk files in this folder and its subfolders
'Usage:
'   ShowSubFolders(strFolder) where strFolder is folder name.
'==========================================================================================================
 
Sub ShowSubFolders(strFolder)
    For Each Subfolder in strFolder.SubFolders
        Set objFolder = objFSO.GetFolder(Subfolder.Path) 
        Set colFiles = objFolder.Files
        For Each objFile in colFiles
          if lcase(right(objFile,3)) = "lnk" Then
             cntLNKFiles = cntLNKFiles + 1
             Readshortcut(objFile)
          end if
        Next
        ShowSubFolders subFolder 'Use recursion to investigate the underlaying levels.
    Next
End Sub
 
 
'==========================================================================================================
'Procedure: getSCPropImage(oFile)
'Target: Collect an image of the shortcut's Properties
'Usage:
'   getSCPropImage oFile
'
'  where oFile is a file object pointing to the shortcut lnk file
'        
'(C) Tom Lavedas 
'==========================================================================================================
 
Sub getSCPropImage(oFile) 
dim sName : sName = replace(lcase(oFile.Name), ".lnk", "")
Dim sPath : sPath = objFSO.GetParentFolderName(oFile) 
Dim oFldr : set oFldr = objApplication.NameSpace(sPath) 
Dim oTemp : set oTemp = oFldr.ParseName(oFile.Name) 
Dim objVerb, colVerbs
If Right(sPath,1)<>"\" Then 
  sPath = sPath & "\" 
End If 
If LOGGING Then 
  WriteToLogFile(" Running the procedure getSCPropImage") 
  WriteToLogFile("    Value 'sPath': " & sPath) 
  WriteToLogFile("    Value 'sName': " & sName) 
  WriteToLogFile("    Result fnFolderItemVerbsVB: " & fnFolderItemVerbsVB) 
End If 
 
dim sVerbName : sVerbName = fnFolderItemVerbsVB
 
If sVerbName <> "" then 
  oTemp.InvokeVerb(sVerbName)
  If LOGGING Then 
    WriteToLogFile("    The verb '" & sVerbName & "' has been found") 
  end if
else
  If LOGGING Then 
    WriteToLogFile("    There is an error - verb '" & sVerbName & "' not found") 
  end if
  exit sub
end if
 
sVerbName = Replace(sVerbName, "&", "")
 
Dim n
 
If LOGGING Then 
   WriteToLogFile("    Processing: " & sName) 
end if
n = 0
 
Set colVerbs = oTemp.Verbs 
For Each objVerb in colVerbs 
    If Replace(objVerb.name, "&", "") = sVerbName Then
       objVerb.DoIt
       if LOGGING Then
          WriteToLogFile("    The command '" & sVerbName & "' has been executed.")
       end if
       do until objShell.AppActivate(sName) or (n > 40) ' wait 2 seconds, only 
          wsh.sleep 50 
          n = n + 1
       loop
    end if
Next
 
if objShell.AppActivate(sName) then
    wscript.sleep 2000  ' Wait 2 seconds so the screen is fully shown. Sometimes there is a delay when showing Windows 7 shortcut properties
    if LOGGING Then 
         WriteToLogFile("    " & sName & " has been found.") 
    end if
    objWordBasic.SendKeys "%{PrtSc}", True ' Other implementations do not support this keystroke
    wscript.sleep 100
    objShell.sendkeys "%{F4}", True        ' Closes Property dialog 
    If LOGGING Then 
         WriteToLogFile("    Details copied to Word (Office) clipboard") 
    end If
    objSelection.Collapse 1 
    On Error Resume Next
    objSelection.Paste 
    if LOGGING then
      if Err.Number = 0 Then
         WriteToLogFile("    Details pasted into Word") 
       else
         WriteToLogFile("    Details NOT pasted into Word due to a Office Clipboard error.")
      end if
    End If 
    On Error Goto 0 
 else
    If LOGGING Then 
      WriteToLogFile("    There is an error while showing the properties for screen '" & sName & "'") 
      WriteToLogFile("    The procedure getSCProperties will be quit.")
      WriteToLogFile("---------------------------------------------------------" & vbCrLf)
    End If 
    Exit Sub 
end if 
 
do until NOT objShell.AppActivate(sName): wscript.sleep 50 :loop 
 
If LOGGING Then 
  WriteToLogFile("---------------------------------------------------------" & vbCrLf) 
End If 
end Sub
 
 
 
'==========================================================================================================
'Procedure: ShowProgress
'Doel: Show a message on the screen.
'Usage:
'  ShowProgress(string)
'where 'string' contains the text to be shown.
'==========================================================================================================
 
Sub ShowProgress(strAction)
    objExplorer.Document.Body.InnerHTML = strAction
End Sub 
 
'==========================================================================================================
'Procedure: WriteToLogFile
'Usage: Write a message in the log file
'Use:
'  ShowProgress(stringwaarde)
'where 'string' contains the text to be written to the log file.
'==========================================================================================================
 
Sub WriteToLogFile(strEntry)
    objOutputFile.WriteLine (Date & " - " & Time & " -> "& strEntry)
End Sub
 
 
'==========================================================================================================
'Function: fnFolderItemVerbsVB()
'Usage: find the string which has to be executed to get the properties.
'Use:
'  fnFolderItemVerbsVB()
'
'==========================================================================================================
 
 
 
function fnFolderItemVerbsVB()
    dim objShell, s
 
    set objShell = CreateObject("Shell.Application")
    if (not objShell is nothing) then
      dim objFolder2
 
      Const ssfWINDOWS = 36
      set objFolder2 = objShell.NameSpace(ssfWINDOWS)
      if (not objFolder2 is nothing) then
        dim objFolderItem
 
        set objFolderItem = objFolder2.Self
        if (not objFolderItem is nothing) then
          dim objItemVerbs
 
          set objItemVerbs = objFolderItem.Verbs
            if (not objItemVerbs is nothing) then
                s = objItemVerbs.item(objItemVerbs.count - 1)
            end if
        end if
      end if
    end if
  fnFolderItemVerbsVB = s
end function

Permanent link to this article: https://www.vroom.cc/wordpress/shortcut-properties/