映射网络驱动器而不在批处理文件中硬编码驱动器号
我需要使用批处理文件映射网络驱动器,但不想指定驱动器号。
批处理文件用作部署过程的一部分;我从 CruiseControl.Net
调用批处理文件,该批处理文件需要映射需要凭据进行身份验证的 UNC 路径。然后,批处理文件调用 RoboCopy 将网站从输出目录部署到目标(并排除一些文件和文件夹)。最后批量删除网络驱动器。
问题是这不可扩展,当只有几个项目时还好,但我们现在有 20 个项目使用这种方法,并且没有足够的驱动器号来映射。我不想重复使用驱动器号,因为它们可能会发生冲突 - 这会很糟糕。
这是批处理文件的示例:
@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
并进行格式化以便于阅读:
@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
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
如果您没有同时连接多个网络共享,您可以让
net use *
为您分配一个空闲驱动器号。之后,您可以使用robocopy
通过其UNC路径访问共享,并使用net use * /delete
释放任何连接的共享。像这样的东西:
编辑:
正如我从一些研究中了解到的,您可以简单地映射共享而不分配驱动器号。然后仅通过其远程 UNC 路径对其进行匿名映射。这样,您还可以通过仅指定其远程名称来删除映射。
这应该有效:
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:
我使用它让 NET 选择一个空闲驱动器号,然后使用 NET 找出它分配的字母:
i use this to let NET pick a free drive letter, then use NET to find out what letter it assigned:
有些人可能会发现以下批处理文件很有用。
它不依赖于外部程序。
该批处理文件包含一个函数
:freedrive
,该函数查找空闲驱动器号并将其返回到变量中。它还可以正确检测没有介质的光驱占用驱动器号。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.可能的解决方案是应用
pushd
命令 指向确实存在的 UNC 路径,因此会创建一个指向该路径的临时驱动器号。可以确定当前目录,即临时驱动器的根目录来获取盘符。最后,必须使用popd
命令删除临时驱动器号:变量
COMPUTERNAME
和共享\\%COMPUTERNAME%\ADMIN$
应该存在于所有现代(基于 NT)的 Windows 系统上。如果您不希望当前环境因
pushd
尝试派生免费驱动器号而受到暂时的“污染”,您可能需要使用以下方法:A possible solution is to apply the
pushd
command to a UNC path that exists for sure, so a temporary drive letter is created that points to that path. The current directory, namely the root of the temporary drive, can be determined to get the drive letter. Finally, thepopd
command must be used to delete the temporary drive letter:The variable
COMPUTERNAME
and the share\\%COMPUTERNAME%\ADMIN$
should exist on all modern (NT-based) Windows systems.If you do not want the current environment to become even temporarily "polluted" by the attempt of
pushd
to derive a free drive letter, you may want to use the following approach:好吧……这可能不那么迷人,但这就是我现在所做的;基本的 try catch 方法。尝试映射驱动器,如果正在使用,则转到下一步。我仅通过 2 次尝试对此进行了说明,但将其扩展到 4 个、10 个或更多驱动器号并不困难。
是的,它确实冒犯了我的编程敏感性,我不喜欢重复代码。不幸的是,我不知道如何将路径和凭据传递到批处理文件中,因为我自己不调用它,CruiseControl.net 调用它时不带参数。
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.
最简单的方法:
Pushd 会自动将网络驱动器映射到可用盘符,并且 popd 将删除它。它们是内部CMD命令;使用 UNC 路径需要启用扩展。批处理示例:
注意:在此使用命令行参数的实现中,必须引用命令行上的路径。
会话示例:
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:
@longneck,我使用这样的东西:
这避免了通过 find 再次调用 net 管道。
您还可以对此进行扩展,这样您就可以在子例程中执行此操作,而不是直接调用 set netdrive。这允许您进行更多的错误检查或处理。
这里的解析子例程并不是很有用,只是说明了这个概念。
@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.