JSI Tip 9099. How can I delete all mapped drives, or all drives mapped to a specified server?


I have scripted DelMapped.bat to delete all mapped drivers, or all drives mapped to one or more servers.

The syntax for using DelMapped.bat is:

DelMapped ALL|Server1 \[Server2 ... ServerN\]

Where:

ALL     deletes all mapped drives on this computer.

ServerX is the NetBIOS name of a computer that one or more drives are mapped to on this computer.
Examples:

Assume the following drive mapping is in effect on your computer:

K:        \\JSI003\JSI003D
L:        \\JSI003\JSI003C
M:        \\JSI005\JSI005C
N:        \\JSI005\JSI005D
P:        \\JSI001\JSI001C
Q:        \\JSI001\JSI001D
If you wish to delete all drive mapping to JSI001 and JSI005, run:

DelMapped JSI001 JSI005

The console output would be:

Deleting P: - \\JSI001\JSI001C
P: was deleted successfully.

Deleting Q: - \\JSI001\JSI001D
Q: was deleted successfully.

Deleting M: - \\JSI005\JSI005C
M: was deleted successfully.

Deleting N: - \\JSI005\JSI005D
N: was deleted successfully.
DelMapped.bat contains:
@echo off
if \{%1\}

\{\} @echo Syntax DelMapped ALL^|Server1 \[Server2 ... ServerN\]&goto :EOF setlocal call :NP>nul 2>&1 :loop if \{%1\}

\{\} endlocal&goto :EOF set srv=%1 set srv=%srv:\=% set fnd=\if /i "%srv%" NEQ "ALL" set fnd=\\%srv% for /f "Tokens=2,3" %%a in ('net use^|FIND ":"^|FIND /I "%fnd%"') do ( set drv=%%a set shr=%%b call :delit ) shift goto loop :NP net use /persistent:NO goto :EOF :delit if "%drv:~1,1%" NEQ ":" goto :EOF @echo Deleting %drv% - %shr% net use %drv% /delete



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish