Samuel Pardee
Samuel Pardee

Reputation: 23

Deleting Desktop Shortcuts Associated With Network Drives?

I've been working to clean up a messy Active Directory as well as a network file system in the same state and I understand the concept of mapping users network drives and currently use a combination of batch and vbs files to do so. However, I need to start fresh and was wondering if there was any way to detect and delete the users shortcuts on their desktop associated with the previous network drives. (Yes - I understand how to delete all of the network drives, but: How do I detect and delete the shortcuts on the desktop associated with them?)

I've already written and custom tailored my own scripts to map drives and place shortcuts. I just need to get rid of any old shortcuts. I can't afford to delete all of the .ink files on the desktop, either. Only any associated with preexisting network drives.

I'm working in an XP / Server 2003 client/server environment.

Another question: If a script runs every time a user logs on through the domain and adds the same network shares over and over again without first deleting them, (even though it would be the same script every time) would it / does it - do any harm? <- I didn't research this one a whole lot yet because i've been crawling through Google and took a peak see through Stack to try and find a solution for the first question/issue.

Any help / advice would be greatly appreciated. Thanks!

Upvotes: 1

Views: 1977

Answers (1)

Mofi
Mofi

Reputation: 49187

Let's say there was before in logon batch

%SystemRoot%\System32\net.exe use Z: \\OldServer\OldShare

and the users created shortcuts to files / directories on this share on their desktop or in a subdirectory of the desktop.

A shortcut file contains always both – the path to the file with drive letter as well as the UNC path with server and share names.

A simple batch file to find in the desktop directory and all its subdirectories *.lnk files containing \\OldServer\OldShare and delete all found shortcut files is:

@echo off
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "\\\\OldServer\\OldShare" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
   echo Deleting shortcut file "%%I"
   del "%%I"
)

For details about /I /M /S run in a command prompt window findstr /?

As it can be seen each backslash in the find string must be escaped with one more backslash.

It is also possible to search for "Z:\\" instead of "\\\\OldServer\\OldShare"

But be careful with deletion of a *.lnk file just based on drive letter because users could have mapped a different share to this drive letter. Those users would not be happy if their shortcuts are deleted, too.

The batch file could ask the user for confirmation before deleting every found shortcut containing the drive letter of not anymore existing drive:

@echo off
setlocal EnableExtensions DisableDelayedExpansion
cls
echo Searching for shortcuts to drive Z: ...
for /F "delims=" %%I in ('%SystemRoot%\System32\findstr.exe /I /M /S "Z:\\" "%USERPROFILE%\Desktop\*.lnk" 2^>nul') do (
   echo/
   echo Shortcut "%%~nI" might be not valid anymore.
   echo/
   setlocal EnableDelayedExpansion
   set Confirm=
   set /P "Confirm=Delete the shortcut (y/n)? "
   if /I "!Confirm!" == "y" (endlocal & del /F "%%I") else endlocal
)
endlocal

It is no problem if a network drive mapping using a command like

%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare

is done in logon batch file on every logon. An error message is output by net use if drive letter Z: is used already, for example if the network drive mapping was done persistent and the user started the computer first without a network connection, then plugged-in the network cable and next after a few seconds entered user name and password to logon to Windows and on domain server of the company.

It is possible to use

%SystemRoot%\System32\net.exe use Z: /delete 2>nul
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare

to first delete an already existing network drive mapping before mapping the share to drive letter Z:. I do not recommend to use wildcard * instead of Z: as that would delete also all network drive mappings created by the user.

For computers not only used in the company network, it is often better to make the drive mapping not persistent by using

%SystemRoot%\System32\net.exe use /persistent:no
%SystemRoot%\System32\net.exe use Z: \\MyServer\MyShare
%SystemRoot%\System32\net.exe use /persistent:yes

Windows does not save in this case in Windows registry under key HKEY_CURRENT_USER\Network that \\MyServer\MyShare should be mapped to Z: and therefore the network drive mapping exists only for current user session. The network drive mapping is removed automatically once Windows is restarted or the user logs off.

Upvotes: 0

Related Questions