We do something similar. We have a batch file on the network that maps the drives a user needs. We update the batch file from time to time, and users run it from a shortcut that we've placed on their desktop:
C:\WINDOWS\system32\cmd.exe /c (@echo off&if not exist \\172.x.x.x\Login (echo Unable to access server&pause) else (md c:\TMP > NUL 2>&1 © \\172.x.x.x\Login\MapDrives.bat C:\TMP /y > NUL 2>&1 &call C:\TMP\MapDrives.bat&del C:\TMP\MapDrives.bat&rd c:\TMP))
You can see that it checks to see if they can access the server, and if they can, it creates a folder C:\TMP
, copies the MapDrives.bat
file locally, then runs it. Since it is running locally, it can remap network drives without terminating it own execution. And we can update the batch file on the server without pushing it to each user's computer.
If you don't want to create a shortcut with the long command line above, it might work to create a second batch file on the server (e.g., RunMe.bat
) that users run from the server. You could place all of the code from the shortcut in the RunMe.bat
and accomplish the same thing. Of course, you'd want to add one more line of code to change to the local drive (so Windows doesn't hold open a handle to the network drive). Something like this:
@echo off
C:
if not exist \\172.x.x.x\Login\MapDrives.bat (
echo Unable to access server
pause
) else (
md c:\TMP > NUL 2>&1
copy \\172.x.x.x1\Login\MapDrives.bat C:\TMP /y > NUL 2>&1
C:\TMP\MapDrives.bat
)
I kept the if not exist ...
because you might place the RunMe.bat
in a different location than the MapDrives.bat
, so it still makes sense to verify the user can access the file. Because I didn't use call C:\TMP\MapDrives.bat
, it transfers control to the local batch file and any handles to the server should be closed so the drive can be remapped. This means however, that you cannot place more commands after the C:\TMP\MapDrives.bat
command.