Windows – Batch File – Delete Shortcut From ALLUSERS Desktop

batch filesymbolic-linkunattendedwindows 7windows-vista

In Vista/7, if I try to delete a shortcut using the following command -:

del "%allusersprofile%\Desktop\MyShortcut.lnk"

…Windows sees this folder as empty and doesn't delete the file.

The environment variable "allusersprofile" points to "C:\ProgramData" however "Desktop" is actually a soft symbolic link to the C:\Users\Public\Desktop folder.

The problem seems to be that these soft links are simply Window Explorer shortcuts and are not recognized by cmd prompts or batch files.

The only solution that I can see is to do the following -:

XP:

del "%allusersprofile%\Desktop\MyShortcut.lnk"

Vista/7:

del "%PUBLIC%\Desktop\MyShortcut.lnk"

Is there any common solution for both OSes?

Best Answer

As stated by Garrett in comments of this question, the only solution I see is as follows:

SET Version=XP

VER | FINDSTR /IL "6.1." > NUL
IF %ERRORLEVEL% EQU 0 SET Version=7

IF %Version% EQU 7  (
 del "%PUBLIC%\Desktop\MyShortcut.lnk"
)
IF %Version% EQU XP  (
 del "%allusersprofile%\Desktop\MyShortcut.lnk"
)

One might note that according to this StackOverflow question, and a blog post by Raymond Chen, a dir of %allusersprofile%\Desktop\<directory> should give the proper results on both XP and 7, however in my experience it does not.

Related Question