Deleting Shortcuts in Windows Vista using VBScript

I am looking to automatically delete a shortcut that is created in Windows Vista when we install a program via MSI.   The following code works perfectly in Windows XP, however errors out in Vista.  

For Vista, the equivalent variable should be:

strShortcut = "C:\Users\Public\Public Desktop\FILENAME.lnk"

Ideally, I would like to use one script to handle both XP and Vista without prompting users or alerting them of its presence.

Offering more points for faster resolution as I need this solved as quickly as is humanly possible.

Thanks.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strShortcut = "C:\Documents and Settings\All Users\Desktop\FILENAME.lnk"
If objFSO.FileExists(strShortcut) Then
   objFSO.DeleteFile(strShortcut)
End If

Open in new window

Metronome09Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Metronome09Connect With a Mentor Author Commented:
Figured out and am a little annoyed at the reason why it wasn't working.  

Basically, the path C:\Users\Public\Public Desktop\FILENAME.lnk won't work directly, however you it is apparently aliased to work with C:\Users\Public\Desktop\FILENAME.lnk.  At least the solution is simple.  I used the IF statements to circumvent any potential errors.  Also, the "Documents and Settings" folder does exist in Windows Vista, but is protected and inaccessible to this script.  To avoid errors, I am checking for the Vista location first and using an ELSE statement to check Windows XP.  If you flip the location of these two and check XP first, you'll find error messages pop up on Vista.  This could probably be remedied, but the script below suits my needs so I won't be working further on it.

@Dinomix--I am using a Windows GPO to run login scripts to execute my VBScript.  The software is installed via GPO as well, however uses a Startup script.  Since startup scripts run first, the software is installed and then the shortcuts are removed.

Hope this code helps others out there.
Set objFSO = CreateObject("Scripting.FileSystemObject")
strShortcut = "C:\Documents and Settings\All Users\Desktop\FILENAME.lnk"
strVistaShortcut = "C:\Users\Public\Desktop\FILENAME.lnk"
 
If objFSO.FileExists(strVistaShortcut) Then
   objFSO.DeleteFile(strVistaShortcut)
 
Else
 
	If objFSO.FileExists(strShortcut) Then
	   objFSO.DeleteFile(strShortcut)
 
	End If
End If

Open in new window

0
 
dinomixCommented:
Instead of using vbs, can you just create a batch file with del "C:\Users\Public\Public Desktop\FILENAME.lnk"

Most likely does not work in vista due to security, maybe you need to run the vbs with admin permissions.
0
 
Metronome09Author Commented:
Upped points to 500 for the solution and/or suggestions that lead me to the solution.

@Dinomix--This solution must work in Vista and I would much prefer to handle it via VBScripts as that is the standard method for everything else that we deploy.

Below is the full script that I have so far:

Set objFSO = CreateObject("Scripting.FileSystemObject")
strShortcut = "C:\Documents and Settings\All Users\Desktop\FILENAME.lnk"
strVistaShortcut = "C:\Users\Public\Public Desktop\FILENAME.lnk"
 
If objFSO.FileExists(strShortcut) Then
   objFSO.DeleteFile(strShortcut)
End If 
If objFSO.FileExists(strVistaShortcut) Then
   objFSO.DeleteFile(strVistaShortcut)
End If 
 
Set objShell = CreateObject("WScript.Shell")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")
strDesktop = objShell.SpecialFolders("Desktop")
If objFSO2.FileExists(strDesktop & "FILENAME.lnk") Then
   objFSO2.DeleteFile(strDesktop & "FILENAME.lnk")
End If

Open in new window

0
 
dinomixCommented:
How do you execute the vbs post install?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.