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

前端 未结 7 1296
无人及你
无人及你 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 19:53

    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.

    @echo off
    
    :START
    net use z: \\192.168.0.1\Share\wwwroot\MyProject /user:mydomain\myuser MyP455w0rd
    if %ERRORLEVEL% ==2 goto Y
    ROBOCOPY HERE
    net use z: /delete
    exit
    
    :Y
    net use y: \\192.168.0.1\Share\wwwroot\MyProject /user:mydomain\myuser MyP455w0rd
    if %ERRORLEVEL% ==2 goto W
    ROBOCOPY HERE
    net use y: /delete
    exit
    
    :W
    sleep 20
    goto START
    

提交回复
热议问题