By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,883 Members | 1,952 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,883 IT Pros & Developers. It's quick & easy.

Getting Shortcuts from users desktop

!NoItAll
100+
P: 296
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.
Expand|Select|Wrap|Line Numbers
  1. . Dim sTargets
  2. . Dim sFileNames
  3. . Set oWMI = GetObject("winmgmts:\\.\root\cimv2")
  4. . set SCs = oWMI.InstancesOf("Win32_ShortcutFile")
  5. . for each SC in SCs
  6. .    If (InStr(1, SC.Target, "font", 1)> 0) Then
  7. .        MsgBox "gotit"
  8. .        sTargets = SC.Target
  9. .        sFileNames = SC.Name
  10. .           Exit For
  11. .          End If
  12. .                 
  13. . Next
  14. . WScript.echo sTargets & vbcrlf & sFileNames
  15.  
This code works - but will not find anything in the users desktop, only in the all users desktop.

Des
Jun 6 '08 #1
Share this Question
Share on Google+
1 Reply


!NoItAll
100+
P: 296
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:

Expand|Select|Wrap|Line Numbers
  1. 'this will find all of the shortcuts on the current users desktop that contain the
  2. 'string in "KillWithTarget" as the target of the shortcut.
  3.  
  4. Dim Shortcut,fso,fldr,fils,fil,WshShell,DeskTopPath,KillWithTarget
  5.  
  6. KillWithTarget="DVSConf.exe"
  7.  
  8. Set WshShell=CreateObject("WScript.Shell")
  9. DesktopPath=WshShell.SpecialFolders("Desktop")
  10.  
  11. Set fso=CreateObject("Scripting.FileSystemObject")
  12. Set fldr=fso.GetFolder(DeskTopPath)
  13. Set fils=fldr.files
  14.  
  15. For Each fil in fils
  16.  
  17.     If Right(fil.path,4) = ".lnk" Then
  18.         Set Shortcut=WshShell.CreateShortcut(fil.path)
  19.         If InStr(1, Shortcut.TargetPath, KillWithTarget, 1) > 0  Then
  20.             fso.deletefile fil.path
  21.         End If
  22.     End If
  23.  
  24. Next
  25.  
  26. 'cleanup
  27. Set WshShell = Nothing
  28. Set fso = Nothing
  29. Set fldr = Nothing
  30. Set fils = Nothing
  31.  
  32.  
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
Jun 10 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.