Smart questions
Smart answers
Smart people
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Member Login




Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Donate Today!

Do you enjoy these
technical forums?
Donate Today! Click Here

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

Delete ShortCut based on target path

vttech (TechnicalUser) (OP)
6 Dec 06 18:30
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

vttech (TechnicalUser) (OP)
6 Dec 06 18:51
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

monsterjta (IS/IT--Management)
6 Dec 06 20:16

CODE

objFSO.DeleteFile(strQLFolder)

What is strQLFolder?

Hope This Helps,

Good Luck!

vttech (TechnicalUser) (OP)
6 Dec 06 23:43
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

vttech (TechnicalUser) (OP)
7 Dec 06 12:35
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

sandtigerclaw (TechnicalUser)
18 May 07 16:08
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 http://www.devguru.com/Technologies/wsh/quickref/wshshell_CreateShortcut.html
                '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

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close