Batch File to Map a Network Drive based on your Default Gateway

Batch File to Map a Network Drive based on your Default Gateway

My laptop spends equal time in the office as it does at home. I have a large music collection that I keep synchronized between the two. I needed a way to automatically mount the appropriate share based on my location. Here's the script. Please take it and use it in your environment.


@Set title=Music Mounter!
@(color 60) & (@Title %title%)
net use m: /delete /y
::# Define a temp variable 'DefaultGateway'
@For /f "tokens=3" %%* in (
   'route.exe print ^|findstr "\"'
   ) Do @Set "DefaultGatewayNHQ=%%*"
::# Define a temp variable 'DefaultGateway'
@For /f "tokens=3" %%* in (
   'route.exe print ^|findstr "\"'
   ) Do @Set "DefaultGatewayZAG=%%*"
if defined DefaultGatewayNHQ (
net use m: "\\10.0.1.3\Music\Full Albums" /user:WORKGROUP\mark password
)
if defined DefaultGatewayZAG (
net use m: "\\10.95.11.122\Music\Albums" /user:DOMAIN\mp3 password
)