Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

Mapping a network drive without hardcoding a drive letter in a batch file

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.1SharewwwrootMyProject /user:mydomainmyuser MyP455w0rd
robocopy.exe "W:wwwrootMyProject" 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.1SharewwwrootMyProject 
    /user:mydomainmyuser MyP455w0rd
robocopy.exe "W:wwwrootMyProject" x: *.* /E /XO /XD 
    "App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
net use x: /delete
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 use robocopy to access the share via its UNC path and release any connected share with net use * /delete.

Something like this:

@echo off
net use * \192.168.0.1SharewwwrootMyProject /user:mydomainmyuser MyP455w0rd
robocopy.exe "W:wwwrootMyProject" "\192.168.0.1SharewwwrootMyProject" *.* /E /XO /XD "App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
net use * /delete /yes

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:

@echo off
net use \192.168.0.1SharewwwrootMyProject /user:mydomainmyuser MyP455w0rd
robocopy.exe "W:wwwrootMyProject" "\192.168.0.1SharewwwrootMyProject" *.* /E /XO /XD "App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
net use \192.168.0.1SharewwwrootMyProject /delete

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...