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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete ShortCut based on target path

Status
Not open for further replies.

vttech

Technical User
Jan 28, 2006
297
US
I want to create a script to delete any desktop shortcut that has a target that starts with \\Serv1\


I was able to create a script to create a shortcut but how can I modify it to delete the shortcut that has a target path that starts with \\Serv1\?


The reason why is because the name can be anything but the path will always start with \\Serv1\



Code:
Set WshShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

strDesktop = WshShell.SpecialFolders("Desktop")



IF Not objFSO.FileExists(strDesktop & "\Company on Serv1.lnk") THEN

set oShellLink = WshShell.CreateShortcut(strDesktop & "\Company on Serv1.lnk")
oShellLink.TargetPath = "\\CHCB8\Company"
oShellLink.WindowStyle = 1

oShellLink.Description = "Shortcut to Company on CHCB8"
oShellLink.WorkingDirectory = "\\Serv1\Company"
oShellLink.Save
set oShellLink = NOTHING
END IF

Set WshShell = NOTHING
Set objFSO = NOTHIN

Newbie in search of knowledge
 
I tried the code below with no luck

Code:
Set WshShell = CreateObject("WScript.Shell")
set objFSO = CreateObject("Scripting.FileSystemObject")

strDesktop = WshShell.SpecialFolders("Desktop")


IF objFSO.FileExists(strDesktop & "\Company on CHCB1.lnk") THEN

objFSO.DeleteFile(strQLFolder)

End If


Set WshShell = NOTHING
Set objFSO = NOTHING

Newbie in search of knowledge
 
Code:
objFSO.DeleteFile(strQLFolder)

What is strQLFolder?

Hope This Helps,

Good Luck!
 
Ok I was able to get the script to work but with a weird effect. It Delete and Creates the intended shortcuts But, the one it deletes it keeps on the desktop but you can't access it when you double click on it; it goes dim. When you reboot the pc the deleted shortcut disappears.

What am I missing??

Code:
Set objShell1 = CreateObject("WScript.Shell")

Set objFSO1 = CreateObject("Scripting.FileSystemObject")

DesktopPath1 = objShell1.SpecialFolders("Desktop")


'if shortcut exist on desktop then delete shortcut

IF objFSO1.FileExists(DesktopPath1 & "\test.lnk") THEN

objFSO1.DeleteFile DesktopPath1 & "\test.lnk"

End If




Set objShell2 = CreateObject("WScript.Shell")
Set objFSO2 = CreateObject("Scripting.FileSystemObject")

DesktopPath2 = objShell2.SpecialFolders("Desktop")


'IF shortcut does not exist then create shortcut
IF Not objFSO2.FileExists(DesktopPath2 & "\Drivers.lnk") THEN

Set link = objShell2.CreateShortcut(DesktopPath2 & "\Drivers.lnk")

'This is the complete path to the shortcut
link.TargetPath = "C:\DRIVERS"

link.Save

End If

Newbie in search of knowledge
 
Ok the script know works but I still have the question how do you make it search for the taget path not the name?

so if a shortcuts target path begins with \\serv1 then it will delete that shortcut. This was if the user rename or add another shortcut it will delete all the shortcuts that begin with \\serv1

Newbie in search of knowledge
 
I came up with the following script to remove the desktop shortcut for all SonicWALL VPN connections based on the target of the shortcuts. It's not professionally written, but then again I'm not a professional. It should serve as a decent example of how to remove a shortcut based on its target path though.

Code:
Dim objShell, objFSO, sonicPath, fileFolder
Dim userFolder, desktopFolder, custFolder, extension
Dim fullname, shortcut, shortTarget

sonicPath = "C:\Program Files\SonicWALL\SonicWALL Global VPN Client\SWGVpnClient.exe"

Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

Set fileFolder = objFSO.GetFolder("C:\Documents and Settings")
Set subFileFolder = fileFolder.Subfolders

For Each userFolder in subFileFolder
	If objFSO.FolderExists(userFolder & "\Desktop") Then
		Set desktopFolder = objFSO.GetFolder(userFolder & "\Desktop")
		Set custFolder = desktopFolder.Files

		For Each custFile in custFolder
			extension = objFSO.GetExtensionName(LCase(custFile.name))
			If extension = "lnk" then
				'Find full path of shortcut
				fullname = objFSO.GetAbsolutePathName(custFile)

				'Find full path of target within shortcut
				'See [URL unfurl="true"]http://www.devguru.com/Technologies/wsh/quickref/wshshell_CreateShortcut.html[/URL]
				'for more information on how this works.
				Set shortcut = objShell.CreateShortcut(fullname)
				shortTarget = shortcut.TargetPath
	
				If InStr(shortTarget, sonicPath) then
					Dim myFileToDelete
					Set myFileToDelete = objFSO.GetFile(fullname)
					myFileToDelete.Delete
				End If
			End If
		Next
	End If
Next
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top