Forum Discussion

lphilippe_macau's avatar
lphilippe_macau
Contributor
13 years ago

Validate a Windows shortcut?

Hi, 



I just ran into a problem with Test Complete. The application I am testing creates shortcuts on the windows deskop during the installation. I want to validate that the shortcuts are pointing to the right path. So basically I want to validate the target property of the shortcut.  I have no idea how to do this with Test Complete.



I tried using;


  • aqFileInfo.Attributes

  • aqFileSystem.CheckAttributes

  • Reading the file with "ReadWholeTextFile" to find a meaningful string inside

  • Readint the file as a binaray file 



None of this seems to work.



The only option right now I can think of is to keep a baseline of the shortcut and validate with file.compare , but still this would not work well when testing different envrionnements. Plus I have to keep one baseline for every shortcut created. 





Do any of you see another way of doing this??

Thank's



L-P

1 Reply


  • Hi Louis-Philippe,





    Here is a script based on the How To sample that demonstrates how to validate the target path of a shortcut on the Desktop:







    Sub Test

      If (Not IsShortcutValid("ShortcutName")) Then

        Call Log.Error("Shortcut is not valid")

        Exit Sub 

      End If

      Call Log.Message("Shortcut is valid")

    End Sub





    Function IsShortcutValid(shortcutName)

      Dim WshShell, fso, startDir, path, shortcut

      Set WshShell = CreateObject("WScript.Shell") 

      startDir = WshShell.SpecialFolders("Desktop") & "\"

      path = startDir & "\" & shortcutName & ".lnk"

      Set fso = CreateObject("Scripting.FileSystemObject") 

      If fso.FileExists(path) Then

        Set shortcut = WshShell.CreateShortcut(path)

        IsShortcutValid =  fso.FileExists(shortcut.TargetPath) 

        Exit Function 

      End If

      IsShortcutValid = False  

    End Function