Good morning, hopefully I have this in the correct topic!

I have a need to delete a specific URL shortcut that would be located on users desktops before copying out a new shortcut to the 'All Users' desktop. I have found the vbscript code below to delete a .lnk from any of the user profiles \desktop folder on the PC which works perfectly. Unfortunately, it's a .url file that needs to be deleted instead. Does anyone know how to modify the script below or have something else that would do the trick????

____________________________________________________________

strComputer = "."

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
    ("Select * From Win32_ShortcutFile Where FileName = 'Name of shortcut to delete'")

For Each objItem in colItems
    If Instr(objItem.Name, "desktop") Then
        strPath = objItem.Name
        strPath = Replace(strPath, "\", "\\")
        Set colFiles = objWMIService.ExecQuery _
            ("Select * From CIM_Datafile Where Name = '" & strpath & "'")
        For Each objFile in colFiles
            objFile.Delete
        Next
    End If
Next

___________________________________________________________________________

Thanks!
SG

Dani AI

Generated

The WMI class you used (Win32_ShortcutFile) only covers .lnk (Windows Shell) shortcuts — .url Internet Shortcuts are plain text files and will not be returned by that class. , that’s why your original WMI query did not find the .url. ’s one‑line batch delete is fine for a single known path, but it doesn’t scale when you need to remove the file from every user profile on the machine.

Below is a compact VBScript that looks for a given .url on the common (All Users / Public) desktop and under each profile folder in both modern (C:\Users) and legacy (C:\Documents and Settings) trees, then deletes it. Run with cscript //nologo DeleteURL.vbs "ShortcutName.url" or edit the default target in the script. Test first and run elevated if you need to remove files from other users’ profiles.

' DeleteURL.vbs
Option Explicit
Dim fso, shell, target, basePaths, i, baseFolderPath, userFld, desktopPath, filePath, commonDesk
Set fso   = CreateObject("Scripting.FileSystemObject")
Set shell = CreateObject("WScript.Shell")

If WScript.Arguments.Count = 0 Then
  target = "Example.url" ' change to the .url name to remove
Else
  target = WScript.Arguments(0)
End If

On Error Resume Next
commonDesk = shell.SpecialFolders("AllUsersDesktop")
If Len(commonDesk) > 0 Then
  filePath = fso.BuildPath(commonDesk, target)
  If fso.FileExists(filePath) Then fso.DeleteFile filePath, True
End If
On Error GoTo 0

basePaths = Array("C:\Users", "C:\Documents and Settings")
For i = 0 To UBound(basePaths)
  baseFolderPath = basePaths(i)
  If fso.FolderExists(baseFolderPath) Then
    For Each userFld In fso.GetFolder(baseFolderPath).SubFolders
      desktopPath = fso.BuildPath(userFld.Path, "Desktop")
      If fso.FolderExists(desktopPath) Then
        filePath = fso.BuildPath(desktopPath, target)
        If fso.FileExists(filePath) Then
          On Error Resume Next
          fso.DeleteFile filePath, True
          If Err.Number <> 0 Then Err.Clear
          On Error GoTo 0
        End If
      End If
    Next
  End If
Next

Notes: run elevated to remove other users’ files; redirected or roaming desktops won’t be found by simple file enumeration (use profile info or Win32_UserProfile / registry for those cases); and when adapting ’s batch idea for multiple users, loop over profile folders rather than hardcoding a single path.

Recommended Answers

All 2 Replies

Instead of using VBS Script code, why don't you use a batch file instead?
one single line of code, then you can just edit that line of code, for example:
"Del "C:\Documents and Settings\C0ding\Desktop\Example.url" >NUL"
Then just run or execute the batch file and thats it.

strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & 
strComputer & "\root\cimv2")

Set colItems = objWMIService.ExecQuery _
("Select * From Win32_ShortcutFile Where
FileName = 'url file to delete'")


For Each objItem in colItems
If Instr(objItem.Name, "desktop") Then
strPath = objItem.Name
strPath = Replace(strPath, "\", "\\")
Set colFiles = objWMIService.ExecQuery _
("Select * From CIM_Datafile Where Name
= '" & strpath & "'")
For Each objFile in colFiles
objFile.Delete
Next1
End If
Next
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.