How Can I Delete a Desktop Shortcut If I Don’t Know the Shortcut’s File Path?

ScriptingGuy1

Hey, Scripting Guy! Question

Hey, Scripting Guy! I need to delete some desktop shortcuts; however, I don’t know which folder those shortcuts might be in. For example, they might be in C:\Documents and Settings\All Users\Desktop or they might be in, say, C:\Documents and Settings\kenmyer\Desktop. How can I delete these shortcuts if I’m not sure where they’re located?

— SB

SpacerHey, Scripting Guy! AnswerScript Center

Hey, SB. You know, this is kind of a bad time; the second half of the UNLV-Georgia Tech game is just about to start. It’s hard enough to try and watch a basketball game at work as it is; to then have to answer a scripting question at the same time is darn near impossible.

On the other hand, Virginia is currently destroying Albany (66-45 with less than 8 minutes remaining), so maybe we can take some time out to answer this question after all. And before you ask, yes, the Scripting Guy who writes this column did pick Albany to upset Virginia, a selection that caused the Scripting Son to take one look at his father’s choices and go, “Look, if you’re not even going to try then why bother making picks in the first place?”

Strong words from the Scripting Son, especially in light of the fact that, after day 1 of the NCAA men’s basketball tournament, the Scripting Dad had the lead over the Scripting Son and the Scripting Brother in their annual March Madness challenge. In fact, if Gonzaga could have made a few shots and if Albany could have – well, OK, made a lot of shots – the Scripting Guy who writes this column would have a perfect score at the moment.

But then again, that’s the story of his life: he’s always this close to being perfect.

Do you realize that, as this column is being written, North Texas only trails Memphis by 6? Unbelievable.

Anyway, on the off-chance that some of you aren’t obsessed with the ups and downs of college basketball, here’s a script that can delete a desktop shortcut regardless of the folder where that shortcut actually resides:

strComputer = “.”

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

Set colItems = objWMIService.ExecQuery _ (“Select * From Win32_ShortcutFile Where FileName = ‘Digital Voice Editor 2′”)

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

Can we explain how this works? Let’s see … well, Virginia just called timeout, so the answer to that is yes, we can explain how this works. (Though we’ll have to do it before the 30-second timeout is over.) What we’ve got here is a desktop shortcut named Digital Voice Editor 2. We want to delete it, but we don’t know for sure which folder the shortcut resides in. Is that a problem? Sure sounds like one, doesn’t it?

As it turns out, though, this is no problem at all. To delete the shortcut we start out by connecting to the WMI service on the local computer. (Although you can easily modify this script to remove desktop shortcuts from a remote computer: all you have to do is assign the name of that computer to the variable strComputer). After making the connection we then use the ExecQuery method and the Win32_ShortcutFile class to return a collection of all the shortcut files that have a FileName of Digital Voice Editor 2:

Set colItems = objWMIService.ExecQuery _
    (“Select * From Win32_ShortcutFile Where FileName = ‘Digital Voice Editor 2′”)

Just a second, SB: Memphis is now up by 13. That’s more what we expected. On the other hand, Albany is now trailing 80-55. In retrospect, picking Albany to win might have been a mistake.

But hey, there’s still 55 seconds left.

Now, back to work. As it always does, the ExecQuery method returns a collection of items; in this case, that’s a collection of all the shortcuts that have the name Digital Voice Editor 2. That means that our next step is to set up a For Each loop that loops through that entire collection:

For Each objItem in colItems

The first thing we do inside that loop is actually an optional step. Because SB specifically mentioned desktop shortcuts we use the InStr function to determine whether the term desktop appears anywhere within the file Name (which, when it comes to the Win32_ShortcutFile class, is equivalent to the file path):

If Instr(objItem.Name, “desktop”) Then

This enables us to remove desktop shortcuts while retaining, say, Start Menu shortcuts. If you’d prefer to delete all the shortcuts to a file (whether or not they happen to be on the desktop) then simply delete the If-Then statement.

And yes, we know: every time UNLV pulls ahead Georgia Tech comes roaring back. Of course, we can’t actually watch that game because CBS insists that either UNLV or Georgia Tech is in the same geographical area as Redmond, Washington. Apparently we don’t know as much about geography as we thought we did.

Geographic Update. The Notre Dame (Indiana) and Winthrop (North Carolina) game is blacked-out for the same geographical reason. OK ….

Now, where were we? Oh, right: inside the For Each loop. As you can see, inside the loop we assign the value of the file Name (which, again, is the same thing as the file path) to a variable named strPath. And then we encounter this line of code:

strPath = Replace(strPath, “\”, “\\”)

Why do we need this line of code? Well, our file path is going to be something like this: C:\Documents and Settings\All Users\Desktop\Digital Voice Editor 2.lnk. There’s nothing wrong with that; that’s what a file path is supposed to look like. However, to actually delete this shortcut we need to use that path in a WMI query. That’s a problem, because the \ is a reserved character and can’t be used in a query without first being “escaped” (which simply means that you need to preface each of those characters with another \).

And that’s what we use the Replace function for: we replace each \ with a \\. That gives us a path (stored in the variable strPath) that looks like this: C:\\Documents and Settings\\All Users\\Desktop\\Digital Voice Editor 2.lnk.

That also gives us a path we can use in a WQL query:

Set colFiles = objWMIService.ExecQuery _
    (“Select * From CIM_Datafile Where Name = ‘” & strpath & “‘”)

What’s this query going to do? It’s going to return a collection of all the files that have a Name (path) equal to the value of the variable strPath. And after it does so we’re going to use this block of code to loop through that collection and delete the corresponding shortcut file:

For Each objFile in colFiles
    objFile.Delete
Next

And then we swing around and repeat the process, just in case there are any other shortcuts named Digital Voice Editor 2 that we need to delete.

We hope that helps, SB. As for the Scripting Guy who writes this column, nothing will help; he’s having a miserable day. At lunch time they had a projector TV set up in the cafeteria. NCAA basketball, right? Wrong: instead, they were showing a cricket match. (Yes, a cricket match.) Not long after lunch the Scripting Guy who writes this column had to go to a meeting, meaning that he missed both Wisconsin’s frenzied comeback against Texas A&M-Corpus-Christi and Nevada’s overtime win over Creighton. Between cricket matches, meetings, and the need to do his job the Scripting Guy who writes this column has hardly had a chance to watch any basketball whatsoever. Apparently Microsoft has lost its sense of priorities.

0 comments

Discussion is closed.

Feedback usabilla icon