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

前端 未结 7 1287
无人及你
无人及你 2020-12-05 19:24

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 batc

7条回答
  •  一向
    一向 (楼主)
    2020-12-05 19:46

    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.1\Share\wwwroot\MyProject /user:mydomain\myuser MyP455w0rd
    robocopy.exe "W:\wwwroot\MyProject" "\\192.168.0.1\Share\wwwroot\MyProject" *.* /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.1\Share\wwwroot\MyProject /user:mydomain\myuser MyP455w0rd
    robocopy.exe "W:\wwwroot\MyProject" "\\192.168.0.1\Share\wwwroot\MyProject" *.* /E /XO /XD "App_Data/Search" "*.svn" /XF "sitefinity.log" "Thumbs.db" /NDL /NC /NP
    net use \\192.168.0.1\Share\wwwroot\MyProject /delete
    

提交回复
热议问题