How to create a batch file to map network drives after connected to VPN

15.9k views Asked by At

What is the best method for creating a batch file (.bat) that will automatically map network drives after a internet connection has been established. The batch file will be remain in the windows start up folder and will only map the network drives if they are not already mapped. I have tried the following:

:Start
ping -n 1 www.google.com

If %errorlevel% == 0 Goto :Start

If %errorlevel% == 1 Goto :Connected

:Connected

Net Use F:\\ Server\Folder /Persistent:No

The ping reply is so fast that the Net use command doesn't map the drive even though the path is correct and will work if I manually enter the net use and path in the CMD window. I need for it to wait after the ping reply and then map the network drives and also for it to only map the network drives if they are not already mapped. I realize what I have tried doesn't address the "only map the network drives if they are not already mapped". I have done some extensive searching and I cannot find the answer to these issues. Would appreciate step by step and easy to follow instructions for resolving this.

Thank you very much.

1

There are 1 answers

2
Radik On

Maybe additional "timeout" can help?

updated

I use windows vpn client. To check available connection i make script like this and wait 30 second before check by ping. And i ping my server which available only under vpn.

@echo off
set myserver=192.168..
setlocal enabledelayedexpansion
rasdial FirstVPN /disconnect
rasdial SecondVPN /disconnect

echo ==== second vpn
start rasdial SecondVPN
timeout /T 30
for /f "Tokens=*" %%G in ('ping -n 1 %myserver% ^| FIND "TTL="') do set ans="%%G"
IF NOT [%ans%]==[] GOTO Connected

echo ==== first vpn
start rasdial FirstVPN
timeout /T 30
for /f "Tokens=*" %%G in ('ping -n 1 %myserver% ^| FIND "TTL="') do set ans="%%G"
IF [%ans%]==[] (exit)
:Connected
echo %ans%