I have a laptop clearly from my job and from all my blogs
One of the things that drives me crazy is having to go from location to location and map drives to access shares. It’s a PITA and annoying. So I have written a batch file to take care of it for me, and I want to share with you guys. This will actually take more than one batch file for simplicity and debugging sakes, but you’ll get the idea pretty quick.
Let’s get started.
(There’s a bunch of ways to create a batch file… This is only one. Do whatever is most comfortable for you)
Step 1: Click Start -> run
Step 2: type:
cd “Start MenuProgramsStartup”
and hit enter
Step 3: Type:
notepad mapdrivesbylocation.bat
Hit enter
Step 4: It will ask you to create the file, hit yes.
Step 5:Here’s where the understanding comes into play, so let’s do some explanation here. The original unmodified code came from here, and it’s what made me spark the idea to do this. At the end it will be highly modified for our needs. Now the early stage of the script is:
@ECHO OFF
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ECHO @ Crimm’s map drive by location@
ECHO @ http://thesmileyking.com @
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@setlocal
SET UNKN=”Unknown host”
SET FAIL=”Lost = 4″
SET RECD=”Received = 4″FOR /F “tokens=1,2,3,4 delims=/ ” %%I IN (‘DATE /T’) DO SET date1=%%J/%%K/%%L
FOR /F “tokens=1,2 delims=: ” %%I IN (‘TIME /T’) DO SET time1=%%I:%%Jping 192.168.0.1 | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown 192.168.0.1ping 192.168.0.1 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure 192.168.0.1ping 192.168.0.1 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation1.batendlocal
What this does is it pings the IP of your choice for that location and if it finds it then it maps the drives for you. Otherwise it tells your failure or unknown. Now you may ask … Why echo the failure/unknown? I did that for troubleshooting purposes. That way I can know if there is an issue to resolve.
Things to point out:
- 192.168.0.1: Is the IP for the gateway at location 1 (You will need to change this to IP’s for your locations)
- e:Scriptslocation1.bat: Is where I keep my scripts for location (You will need to change this to your location batch files – See below)
The rest you can look up on your own
Now to add more locations just add the stuff in the middle, for example to add a second location copy/paste this and change IP’s:
ping 192.168.1.1 | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown 192.168.1.1ping 192.168.1.1 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure 192.168.1.1ping 192.168.0.101 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation2.bat
Continue doing this for all of your locations giving us:
@ECHO OFF
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ECHO @ Crimm’s map drive by location@
ECHO @ http://thesmileyking.com @
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
setlocalSET UNKN=”Unknown host”
SET FAIL=”Lost = 4″
SET RECD=”Received = 4″FOR /F “tokens=1,2,3,4 delims=/ ” %%I IN (‘DATE /T’) DO SET date1=%%J/%%K/%%L
FOR /F “tokens=1,2 delims=: ” %%I IN (‘TIME /T’) DO SET time1=%%I:%%Jping 192.168.0.1 | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown 192.168.0.1ping 192.168.0.1 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure 192.168.0.1ping 192.168.0.1 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation1.batping 192.168.1.1 | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown 192.168.1.1ping 192.168.1.1 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure 192.168.1.1ping 192.168.1.1 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation2.batping 192.168.2.1 | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown 192.168.2.1ping 192.168.2.101 | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure 192.168.2.1ping 192.168.2.1 | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation3.bat
endlocal
Now when you go home, or when you go to another customer’s location … it will ping that location that you set and map a drive for you. But wait … We can do better than this. We can make this leaner, cleaner and better to utilize. Before we get into the mapping of the drives, let’s work on our script a bit more. First let’s make those locations into variables to make it easier to add more locations by adding this bit:
SET HOME=192.168.1.1
SET WORK=192.168.0.1
SET WORK2=192.168.2.1
NOTE: Make sure there are no spaces around those = marks!
Also let’s add some remarks to this batch file so that we can look at this script easily and add more locations easily. Plus it’s always a good idea to comment your work!!!
Finally let’s add a few echoes so that we know where we are in the script as it goes along by adding these:
ECHO Looking for: %WORK%
ECHO Looking for: %WORK2%
ECHO Looking for: %HOME%
Which gives us a new script of:
@ECHO OFF
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
ECHO @ Crimm’s map drive by location@
ECHO @ http://thesmileyking.com @
ECHO @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@setlocal
SET UNKN=”Unknown host”
SET FAIL=”Lost = 4″
SET RECD=”Received = 4″
SET HOME=192.168.1.1
SET WORK=192.168.0.1
SET WORK2=192.168.2.1FOR /F “tokens=1,2,3,4 delims=/ ” %%I IN (‘DATE /T’) DO SET date1=%%J/%%K/%%L
FOR /F “tokens=1,2 delims=: ” %%I IN (‘TIME /T’) DO SET time1=%%I:%%JREM @@@@@@@@@@@@@@@@@@@@@@@
REM @ Location 1 @
REM @@@@@@@@@@@@@@@@@@@@@@@
ECHO Looking for: %WORK%
ping %WORK% | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown %WORK%ping %WORK% | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure %WORK%ping %WORK% | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation1.batREM @@@@@@@@@@@@@@@@@@@@@@@
REM @ Location 2 @
REM @@@@@@@@@@@@@@@@@@@@@@@
ECHO Looking for: %HOME%
ping %HOME% | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown %HOME%ping %HOME% | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure %HOME%ping %HOME% | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation2.batREM @@@@@@@@@@@@@@@@@@@@@@@
REM @ Location 3 @
REM @@@@@@@@@@@@@@@@@@@@@@@
ECHO Looking for: %WORK2%
ping %WORK2% | FIND /I %UNKN% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] unknown %WORK2%ping %WORK2% | FIND /I %FAIL% >NUL
IF NOT ERRORLEVEL 1 ECHO [%date1% %time1%] failure %WORK2%ping %WORK2% | FIND /I %RECD% >NUL
IF NOT ERRORLEVEL 1 e:Scriptslocation3.batendlocal
Step 6:
I know that most of you could improve on that, but I t
hink I’m happy with it just the way it is. It’s simple, it’s clean, it’s fast, it’s lean. No fuss no muss. Now we need to move onto mapping the drives. As I stated before the script above has this:
e:Scriptslocation1.bat
You need to create a new batch file like we did in the beginning but place it wherever you want and replace those lines above. Just *REMEMBER* if you place your batch file in a location with spaces in it… i.e. c:documents and settingscrimmlocation 2.bat … YOU HAVE TO PUT quotes around it. So open your map drives bat and let’s get started.
First remember to remove any map drives that might be cached with the following line:
IF EXIST g: net use g: /delete
IF EXIST h: net use h: /delete
G: and H: being an arbitrary set of drives. You can make it/them whatever you want and add as many as you want. Just remember to add all the ones you need to remove all the drives you will map! Otherwise you will get failures and errors and System Error 85′s. The IF EXISTS line in there keeps you from getting this error: DRIVE DOES NOT EXIST. This just keeps the output pretty. Next map your drive (s):
net use g: “\192.168.0.254c$” PASSWORDHERE /user:”DOMAINNAMEUSERNAME”
net use h: “\192.168.0.254d$” PASSWORDHERE /user:”DOMAINNAMEUSERNAME”
Replace PASSWORDHERE and DOMAINNAME and USERNAME respectively. I hope you know what to put there
… If your drive isn’t encrypted and you don’t want to store your password … Then leave it out. It’s probably best if you leave it out anyway. When the script runs it will just ask you for your password. Finally add a pause an echo to leave it up to see errors and pat your self on the back, giving us:
@ECHO OFF
REM @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
REM @ Crimm’s map drive by location@
REM @ http://thesmileyking.com @
REM @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@REM Removing mapped drives
IF EXIST g: net use g: /delete
IF EXIST h: net use h: /deleteREM Mapping our drives
net use g: “\192.168.0.254c$” PASSWORDHERE /user:”DOMAINNAMEUSERNAME”
net use h: “\192.168.0.254d$” PASSWORDHERE /user:”DOMAINNAMEUSERNAME”ECHO We are done.
pause
That’s it. Create all the location batch files you need for each location from the first script and you will get automatically mapped drives when you login at each location. Now you need to note:
When an IP is successfully pinged, then the location script takes over! Therefore you can only have one location at a time with this set of scripts. That meets my needs, but may not meet yours, but I figure if you need to map drives to more than one IP scheme per location you can just map them in your location map drive batch script.
Please let me know if you have any questions or need any help at http://blog.thesmileyking.com or http://forum.thesmileyking.com
Thanks and I hope you learned something today.
http://blog.thesmileyking.com



