映射网络驱动器而不在批处理文件中硬编码驱动器号 [英] Mapping a network drive without hardcoding a drive letter in a batch file

查看:29
本文介绍了映射网络驱动器而不在批处理文件中硬编码驱动器号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要用批处理文件映射网络驱动器,但不想指定驱动器号.

I need to map a network drive with a batch file, but don't want to specify the drive letter.

批处理文件用作部署过程的一部分;我从 CruiseControl.Net 调用批处理文件,批处理文件需要映射一个需要凭据进行身份验证的 UNC 路径.然后批处理文件调用RoboCopy 将网站从输出目录部署到目的地(并排除一些文件和文件夹).最后批量删除网络驱动器.

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.

问题是这不是可扩展的,当只有几个项目时还好,但我们现在有 20 个项目使用这种方法,并且要映射的驱动器号用完了.我不想重复使用驱动器号,因为它们可能会发生冲突 - 这会很糟糕.

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

推荐答案

如果你没有同时连接多个网络共享,你可以让 net use * 为你分配一个空闲的盘符.之后,您可以使用 robocopy 通过其 UNC 路径访问共享,并使用 net use */delete 释放任何连接的共享.

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.

像这样:

@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

我从一些研究中了解到,您可以简单地映射共享,而无需分配驱动器号.然后匿名映射,仅通过其远程 UNC 路径.通过这种方式,您还可以通过仅指定其远程名称来删除映射.

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.

这应该有效:

@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

这篇关于映射网络驱动器而不在批处理文件中硬编码驱动器号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆