Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Desktop Shortcut (only if it exists, otherwise carry on)

Status
Not open for further replies.

slobad23

IS-IT--Management
Jun 30, 2006
90
GB
I have used a VB script to create a shortcut on everyones desktop. This worked fine and I create a url object. To create my own icon it needed to be changed to an lnk file. Now all the users have the old shortcut as well as the new one with my own icon.

I create a VB script to run at login to delete that file but it works the first time and will create errors afterwards. I can't know for sure when certain users are going to be logging in so I can't put it on there then delete it otherwise it will not work for everybody.

What I need to do is check for this one unique file name on the desktop. If it exists, I want to delete it. If it doesn't I want the script to carry on without popping errors up and people.

I found this on tek-tips:

Code:
Path1 = "C:\Program Files\HEAT"



Dim fso 
Dim oFolder
Dim oFile
Dim oSubFolder

  Set fso = createobject("Scripting.FileSystemObject")
  
   Set oFolder = fso.GetFolder(Path1)
  
  For Each oFile In oFolder.files
       If Left(oFile.Name,3)= "HFW" And Right(lcase(oFile.Name,3)) = "tmp" Then
        oFile.Delete True
    End If
  Next

but this deletes all files with a certain extension and I don't want to delete all url files on everyones desktop. Could anyone tell me or point me to a website that explains how to do this?

Thanks in advance
 
You could also try something like this:
Code:
On error resume next
Dim shell, fldrs, shcut
Dim fso, sdesktop, sOldFilename
set fso=WScript.CreateObject("Scripting.FileSystemObject")
set shell=WScript.CreateObject("WScript.Shell")
sdesktop=shell.SpecialFolders("AllUsersDesktop")
set sOldFilename=fso.GetFile(sdesktop & "\Old Web Site.lnk")
sOldFilename.Delete

set fldrs=shell.SpecialFolders
set shcut=CreateShortcut("AllUsersDesktop", "New Web Portal.lnk", _
   "[URL unfurl="true"]http://server/index.htm")[/URL]

Function CreateShortcut(sFolder, sFilename, sTarget)
   Dim fso, shell, sFolderPath, sFullPath
   set fso=WScript.CreateObject("Scripting.FileSystemObject")
   set shell=WScript.CreateObject("WScript.Shell")
   sFolderPath=shell.SpecialFolders(sFolder)
   sFullPath=sFolderPath & "\" & sFilename
   if fso.FileExists(sFullPath) Then 
     WScript.Quit
   End if
   set CreateShortcut=shell.CreateShortcut(sFullPath)
   CreateShortcut.TargetPath=sTarget
   CreateShortcut.IconLocation="%SystemRoot%\system32\SHELL32.dll, 13"
   CreateShortcut.Save
End Function
WScript.Quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top