1.

Solve : VBS - Remove Desktop Shortcut from any user?

Answer»

Code: [Select]'http://www.tek-tips.com/viewthread.cfm?qid=1309939&page=9
Dim objShell, objFSO, sonicPath, fileFolder
Dim userFolder, desktopFolder, custFolder, extension
Dim fullname, shortcut, shortTarget

Set objArgs = WScript.Arguments
targetpath = objArgs(0)
StartDir = objArgs(1)
'MsgBox Chr(34) & targetpath & Chr(34)
sonicPath = Chr(34) & targetpath & Chr(34)
sonicDir = Chr(34) & StartDir & Chr(34)

'ON Error resume next

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

MsgBox sonicDir & " " & StartDir & " " & SonicPath

'Set fileFolder = objFSO.GetFolder("C:\Documents and Settings")
Set fileFolder = objFSO.GetFolder(sonicDir)
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
'msgbox desktopFolder
For Each custFile in custFolder
extension = objFSO.GetExtensionName(LCase(custFile.name))
If extension = "url" Or 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
'msgbox ShortTarget
If InStr(shortTarget, sonicPath) then
Dim myFileToDelete
Set myFileToDelete = objFSO.GetFile(fullname)
myFileToDelete.Delete
End If
End If
Next
End If
Next
I didn't write the above (LINK AT TOP) but am curious in expanding the functionality to allow for %SystemDrive%\Doc&Settings or \Users the path (VAR:StartDir). I am using a BATCH file to call the VBS passing it the full pathname of the shortcut with the %StartDir% But keeping getting path not found. I'm sure it is something SIMPLE but I'm not seeing it.

BAT FILE
Code: [Select]FOR /F "tokens=1-2 delims=\" %%A in ('ECHO %APPDATA%') do (
SET drive=%%A
SET PATH=%%B
)

REM ECHO %DRIVE%\%PATH% & PAUSE

DeleteShortCuts.vbs "http://companyaddress.com" "%DRIVE%\%PATH%"
DeleteShortCuts.vbs "https://companyaddress.com" "%DRIVE%\%PATH%"
The reason for doing this is the companyaddress.com has changed and the shortcuts no longer work. I have created a new version to drop a new URL onto the client machine using the ALLUsersDesktop special FOLDER in VBS but I want to be able to remove any old shortcut from any %USERNAME%\DESKTOP folder. Avoid confusion ... avoid clutter etc.

thanks for the assist. Looks like I waited to long to modify the code. I GOT this mostly working ....

Code: [Select]'http://www.tek-tips.com/viewthread.cfm?qid=1309939&page=9
Dim objShell, objFSO, sonicPath, fileFolder
Dim userFolder, desktopFolder, custFolder, extension
Dim fullname, shortcut, shortTarget

Set objArgs = WScript.Arguments
targetpath = objArgs(0)
StartDir = objArgs(1)
'MsgBox Chr(34) & targetpath & Chr(34)
'onicPath = Chr(34) & targetpath & Chr(34)
'sonicDir = Chr(34) & StartDir & Chr(34)
sonicPath = objArgs(0)
sonicDir = objArgs(1)

On Error resume next

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

'MsgBox sonicDir & " " & StartDir & " " & SonicPath

'Set fileFolder = objFSO.GetFolder("C:\Documents and Settings")
Set fileFolder = objFSO.GetFolder(sonicDir)
Set subFileFolder = fileFolder.Subfolders
'On Error resume Next
For Each userFolder in subFileFolder
If objFSO.FolderExists(userFolder & "\Desktop") Then
Set desktopFolder = objFSO.GetFolder(userFolder & "\Desktop")
Set custFolder = desktopFolder.Files
'msgbox desktopFolder.Files
'MsgBox custFolder.Count
MsgBox desktopFolder
For Each custFile in custFolder
'MsgBox CustFile.Name
extension = objFSO.GetExtensionName(LCase(custFile.name))
If extension = "url" Or 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)
Set WshShell = WScript.CreateObject("WScript.Shell")
set oShellLink = WshShell.CreateShortcut(fullname)
shortTarget = shortcut.TargetPath
shortTarget1 = oShellLink.TargetPath

msgbox shortTarget & " " & fullname
If InStr(shortTarget, sonicPath) then
Dim myFileToDelete
Set myFileToDelete = objFSO.GetFile(fullname)
myFileToDelete.Delete
End If
End If
Next
End If
Next

....but on line 51 Code: [Select]msgbox shortTarget & " " & fullname
shortTarget sometimes reports a blank target even THOUGH if I right click on the file and go to properties to view it I can see the target. I'm wondering if it is because the LNK file is a shortcut to a webaddress instead of the LNK file being a shortcut to an application. Would there be a WAY to fix this so I can get shortTarget even if the target is a URL in a LNK file. I need this same script but need to be able to search the entire c drive and remove. Could someone possibly help me with that?

Thanks
Chad



Discussion

No Comment Found