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

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.