Greg's Tech blog


Thursday 27 of March, 2008

For our corporate logon script, we wanted to make sure all net use commands succeeded even if a drive was already mapped. We developed the following script to do this.

::Setdrive.cmd GjM 3/26/08
:: Unsets and sets a drive mapping
:: Usage:  Setdrive  
:: Ex: Setdrive P \\myco.com\Corp
@echo off
::The for /f acts to split the drive letter from the colon so we don't wind up with k::
for /f "delims=:" %%a in ("%1") do (
	:: if the drive is mapped, delete it and remap it; otherwise just map it
	if exist %a:\ (
		Net use %%a: /del 
		Net use %%a: %2%3%4 
	   ) else (
		Net use %%a: %2%3%4

Here's how you might use this. Note: the setdrive script deals with the presence or lack of the colon after the drive letter.

:: **********************************************************
:: Map Corporate shares
call setdrive H \\myco.com\Corp\All_HomeDIR\%username%
call setdrive I \\myco.com\Corp

It's well commented so I'll leave it at that. Feel free to borrow it.