You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用IF语句结合Net Use编写批处理实现远程盘符优先级映射

Batch Script for Priority Drive Mapping (D$ → E$ Fallback)

Got it, here's a straightforward batch script that handles your exact requirement: it first tries mapping to the D$ share's FolderName, and automatically switches to E$ if the initial attempt fails. Let's dive into it:

@echo off
setlocal enabledelayedexpansion

:: Configure your target details - tweak these to match your setup
set "TARGET_SERVER=server"
set "FIXED_FOLDER=FolderName"
set "MAP_DRIVE=Z:"  :: Pick a drive letter that's not currently in use

:: First attempt: Map to the D$ share
echo Trying to map %MAP_DRIVE% to \\%TARGET_SERVER%\D$\%FIXED_FOLDER%...
net use %MAP_DRIVE% "\\%TARGET_SERVER%\D$\%FIXED_FOLDER%" /persistent:no

:: Check if the first mapping failed
if errorlevel 1 (
    echo D$ share mapping failed. Switching to E$ fallback...
    :: Clean up any partial mapping to avoid conflicts
    net use %MAP_DRIVE% /delete /y >nul 2>&1
    :: Attempt mapping the E$ share
    net use %MAP_DRIVE% "\\%TARGET_SERVER%\E$\%FIXED_FOLDER%" /persistent:no
    
    :: Verify if the fallback worked
    if errorlevel 1 (
        echo Error: Couldn't map either D$ or E$ shares on %TARGET_SERVER%
        pause
        exit /b 1
    ) else (
        echo Success! %MAP_DRIVE% mapped to \\%TARGET_SERVER%\E$\%FIXED_FOLDER%
    )
) else (
    echo Success! %MAP_DRIVE% mapped to \\%TARGET_SERVER%\D$\%FIXED_FOLDER%
)

endlocal

Important Notes & Tips:

  • Variable Adjustments: Update TARGET_SERVER, FIXED_FOLDER, and MAP_DRIVE to fit your actual environment. Make sure MAP_DRIVE is an unused drive letter on your domain-joined machine.
  • Error Handling: The script checks the errorlevel returned by net use—any value ≥1 means the mapping failed. We clean up partial mappings before trying the fallback to prevent drive letter conflicts.
  • Permissions: Since your machine uses domain login and the remote server is locally logged in, confirm your domain user has read/write access to both the D$ and E$ administrative shares on the remote server.
  • Persistent Mapping: The /persistent:no flag ensures the mapping doesn't stay after you log off. Remove this flag if you want the mapping to persist across sessions.

If you hit snags, double-check:

  • The remote server is online and reachable over the network
  • FolderName exists on either the D: or E: partition of the remote server
  • Your domain account has the necessary permissions to access the administrative shares

内容的提问来源于stack exchange,提问作者Aaron

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:22:14