Introduction
Although Microsoft calls it mapping a drive, in truth, you are just mapping the location of a specific local or remote folder. You are not technically mapping an entire hard drive.
There are several reasons it is worthwhile to write a batch script that can auto connect your frequently used folders. A common reason is that enterprise users frequently get their network folders disconnected. Problems arise from VPN disconnects, power fluctuations, or other concerns.
We can make a batch script and save it on their desktop (or in their startup folder) to quickly get their folders re-connected. The user’s just need to double click the script file and it will quickly restore their network folders.
Map the folders (aka. drives)
Let’s create a script called ‘mapdrives.bat’ using notepad.
Add information to the headers. Anything with REM or :: will not be executed.
REM mapdrives.bat
REM Place this file on your desktop, in your startup folder, or create a task.
TITLE MAP NETWORK & LOCAL FOLDERS
Use ‘echo off’, at the top of the script. This tells the script to not display the commands or results to the screen, as they are executed. Use ‘echo.’ to print a blank line, and use just ‘echo’ to print data to the screen.
@echo off
cls
echo ******************************************
date /t
echo.
echo Mapping folders . . .
echo.
Comment your code using descriptors and use ‘net use’ to map the folder paths. Be sure to put quotes around the file path if there is a space somewhere in the path.
REM Map a Network Folder.
net use S: \\server_name\folder\folder
net use H: \\website.com\folder\folder
REM Map a local folder called scripts.
net use N: "\\localhost\c$\Users\<username>\Cool Stuff\scripts"
echo.
Let’s create a short delay and exit the program. We can add the command ‘pause’ and it will hold the command prompt open until a key is struck or use ‘ping’ and it will wait three seconds and exit automatically.
echo All folders are mapped - End of Task
:: This will create a 3 sec delay before exiting.
:: ping 127.0.0.1 -n 3 -w 1000 > NUL
pause
echo.
exit
Finally, save the file with ‘.bat’ file extension and then double click the file to execute it.
Open File Explorer and you will see your newly mapped folders.
