Saturday, July 25, 2009

Windows XP and Vista - Map Network Drive with Batch file

For security reason it is a good practice to share specific folders on network with authorized access. Unfortunately Windows sharing has an unpleasant feature that if there is an established connection from one user to a network computer (sharing server), connection for another user from the same machine to the same server will be rejected. For instance if there are two shared folders: one for User1 and another for User2,- and User1 has already connected, in order to switch to User2 it is necessary to close the User1 network mapping first then reconnect as User2. Not a big deal to do it once in a while, just go to Windows Explorer's Tools\"Disconnect Network Drive" then "Map Network Drive", however switching back and forth becomes annoying very quickly. A possible solution is to use a batch file to automate the switching.

The command to handle that is "net use..." with different options. A description of the command can be found on Microsoft website: Net use .

The two batch files can do the job:
  1. User1.bat has the following lines:

    net use * /delete /yes
    net use x: \\NetServerName\SharedFolder *
    /user:NetServerName\User1

  2. User2.bat :

    net use * /delete /yes
    net use y: \\NetServerName\SharedFolder *
    /user:NetServerName\User2
The first command "net use * /delete /yes" in the both files deletes all existing connections. The key "/yes" is not documented in the official Microsoft description, but it works as a default response to the command prompt question [Y/N].

The second command sets up new mapping to the specified shared folder from the specified user. An asterisk (*) at the password key position makes the command ask for the password.

I put shortcuts to the batch files on the desktop and switching between these shared folders became trivial.

0 comments:

Post a Comment