问题
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!
回答1:
Let's say there was before in logon batch
net 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 desktop directory and all its subdirectories *.lnk files containing \\OldServer\OldShare
and delete all found shortcut files is
@echo off
for /F "usebackq delims=" %%F in ( `%SystemRoot%\system32\findstr.exe /I /M /S "\\\\OldServer\\OldShare" "%USERPROFILE%\Desktop\*.lnk"` ) do (
echo Deleting shortcut file "%%F"
del "%%F"
)
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
cls
echo Searching for shortcuts to drive Z: ...
setlocal ENABLEDELAYEDEXPANSION
for /F "usebackq delims=" %%F in ( `%SystemRoot%\system32\findstr.exe /I /M /S "Z:\\" "%USERPROFILE%\Desktop\*.lnk"` ) do (
echo.
echo Shortcut "%%~nF" might be not valid anymore.
echo.
set Confirm=
set /p "Confirm=Delete the shortcut (y/n)? "
if /i "!Confirm!"=="y" (
attrib -R "%%F"
del "%%F"
)
)
endlocal
It is no problem if a network drive mapping using a command like
net 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
net use Z: /delete 2>nul
net 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
net use Z: \\MyServer\MyShare /persistent:no
Now Windows does not save in Windows registry that \\MyServer\MyShare
should be mapped automatically 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.
来源:https://stackoverflow.com/questions/24704617/deleting-desktop-shortcuts-associated-with-network-drives