I need to map a network drive with a batch file, but don't want to specify the drive letter.
The batch file is used as part of a deployment process; I call the batch file from CruiseControl.Net
, the batch file needs to map a UNC path which requires credentials to authenticate. Then the batch file calls RoboCopy
to deploy the website from the output directory to the destination (and excludes some files and folders). Finally the batch deletes the network drive.
The problem is that this isn't scalable, it's fine when there are only a few projects but we've now got 20 projects using this approach and are running out of drive letters to map. I don't want to re-use drive letters as they could collide - which would be bad.
This is an example of the batch file:
@echo off
net use x: \\192.168.0.1\Share\wwwroot\MyProject /user:mydomain\myuser MyP455w0rd
robocopy.exe "W:\wwwroot\MyProject" x:\ *.* /E /XO /XD "App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
net use x: /delete
and formatted for readability:
@echo off
net use x: \\192.168.0.1\Share\wwwroot\MyProject
/user:mydomain\myuser MyP455w0rd
robocopy.exe "W:\wwwroot\MyProject" x:\ *.* /E /XO /XD
"App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
net use x: /delete
Some may find the following batch file useful.
It does not rely on external programs.
The batch file contains a function
:freedrive
, which finds a free drive letter and returns it in a variable. It also correctly detects optical drives that have no media as occupying a drive letter.i use this to let NET pick a free drive letter, then use NET to find out what letter it assigned:
Ok... this might not be glamourous but this is how I'm doing this now; a basic try catch approach. Try to map a drive and if it's in use then goto the next step. I've illustrated this with just 2 attempts, but it's not hard to extend it to 4, 10 or more drive letters.
Yes it does offend my programming sensibilities, I don't like the repetion of code. Unfortunately I don't know how I could pass the path and credentials into the batch file as I don't call it myself, CruiseControl.net calls it without parameters.
@longneck, I use something like this:
This avoids the second call to net piped through find.
You can also expand on this so that instead of calling set netdrive directly, you do it in a subroutine. This allows you to do more error checking or processing.
The parse subroutine here isn't terribly useful and only illustrates the concept.
If you don't have multiple network shares connected simultaniously, you can make
net use *
assign a free drive letter for you. Afterwards you can userobocopy
to access the share via its UNC path and release any connected share withnet use * /delete
.Something like this:
EDIT:
As I learned from some researches, you can simply map the share without assigning a drive letter. It is then mapped anonymously, only by its remote UNC path. This way you can also remove the mapping by specifiying only its remote name.
This should work:
Simplest method:
Pushd will automatically map a network drive to an available letter, and popd will remove it. They are internal CMD commands; using UNC paths requires extensions enabled. Batch example:
Note: in this implementation using a command line argument, the path on command line must be quoted.
Sample session: