Getting Shortcuts from users desktop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • !NoItAll
    Contributor
    • May 2006
    • 297

    Getting Shortcuts from users desktop

    Hi:
    I'm using the following script to find a shortcut with a specific string in it. In the code below the example is the word "font"
    My problem is that this code looks everywhere EXCEPT the individual users desktop - it does find all of the shortcuts in All Users, but not in specific users.
    I am executing the code AS the user whose desktop I need to search so there shouldn't be a security issue.
    I have to write a small routine to add to an installer that will delete and old shortcut from the user desktop so I would really appreciate some help.
    [code=vb]
    . Dim sTargets
    . Dim sFileNames
    . Set oWMI = GetObject("winm gmts:\\.\root\c imv2")
    . set SCs = oWMI.InstancesO f("Win32_Shortc utFile")
    . for each SC in SCs
    . If (InStr(1, SC.Target, "font", 1)> 0) Then
    . MsgBox "gotit"
    . sTargets = SC.Target
    . sFileNames = SC.Name
    . Exit For
    . End If
    .
    . Next
    . WScript.echo sTargets & vbcrlf & sFileNames
    [/code]

    This code works - but will not find anything in the users desktop, only in the all users desktop.

    Des
    Last edited by Dököll; Jun 6 '08, 11:46 PM. Reason: [code=vb]
  • !NoItAll
    Contributor
    • May 2006
    • 297

    #2
    Well, I solved my own problem. As it turns out the method I was using has no options and therefore can not do what I need. Besides, it always searches the entire system and that can be very slow - so a new approach was required.
    Here's the code:

    Code:
    'this will find all of the shortcuts on the current users desktop that contain the
    'string in "KillWithTarget" as the target of the shortcut.
    
    Dim Shortcut,fso,fldr,fils,fil,WshShell,DeskTopPath,KillWithTarget
    
    KillWithTarget="DVSConf.exe"
    
    Set WshShell=CreateObject("WScript.Shell")
    DesktopPath=WshShell.SpecialFolders("Desktop")
    
    Set fso=CreateObject("Scripting.FileSystemObject")
    Set fldr=fso.GetFolder(DeskTopPath)
    Set fils=fldr.files
    
    For Each fil in fils
    
    	If Right(fil.path,4) = ".lnk" Then
    		Set Shortcut=WshShell.CreateShortcut(fil.path)
    		If InStr(1, Shortcut.TargetPath, KillWithTarget, 1) > 0  Then
    			fso.deletefile fil.path
    		End If
    	End If
    
    Next
    
    'cleanup
    Set WshShell = Nothing
    Set fso = Nothing
    Set fldr = Nothing
    Set fils = Nothing
    So this looks in the user desktop, finds all shortcuts, then deletes any whose target application has a specific string (KillWithTarget ) - case insensitive.
    This makes an odd use of the createshortcut method - which is normally used to actually create a NEW shortcut. As it turns out if you call it on an existing shortcut it will open it and expose all of it's properties to you instead.

    Des

    Comment

    Working...