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

编写可读取mapped_drives.txt生成并修改新批处理文件的脚本

Batch Script to Generate Drive Mapping Script from mapped_drives.txt

Alright, let’s tackle this problem head-on. Since you’re moving from Windows 7 to 10 without roaming profiles, we’ll build a batch script that reads the mapped_drives.txt file (saved after logoff) and generates a reusable script to remap those drives automatically.

How It Works

This script will:

  • Check if your mapped_drives.txt exists to avoid errors
  • Skip empty lines in the input file to prevent invalid commands
  • Generate net use commands to first remove existing drive mappings (to fix conflicts) and then remap them
  • Save all these commands into a new, ready-to-run batch file

The Full Batch Script

@echo off
setlocal enabledelayedexpansion

:: Define input/output file names
set "inputFile=mapped_drives.txt"
set "outputFile=AutoMapDrives.bat"

:: Verify input file exists
if not exist "%inputFile%" (
    echo Error: %inputFile% not found in the current directory!
    pause
    exit /b 1
)

:: Initialize the output batch file
echo @echo off > "%outputFile%"
echo echo Mapping network drives... >> "%outputFile%"
echo. >> "%outputFile%"

:: Read each line from the input file
for /f "tokens=* delims=" %%a in ('type "%inputFile%"') do (
    :: Skip empty lines
    if not "%%a"=="" (
        :: Split line into drive letter and network path (handles spaces)
        for /f "tokens=1,*" %%b in ("%%a") do (
            :: Add command to delete existing mapping
            echo net use %%b /delete /y >> "%outputFile%"
            :: Add command to remap the drive (quotes handle space-containing paths)
            echo net use %%b "%%c" /persistent:yes >> "%outputFile%"
            echo. >> "%outputFile%"
        )
    )
)

:: Add completion message and pause
echo echo Drive mapping completed! >> "%outputFile%"
echo pause >> "%outputFile%"

echo Successfully generated %outputFile%!
pause
endlocal

Key Notes

  • Input File Format: Your mapped_drives.txt must have one mapping per line, formatted like Z: \\server01\user_documents (drive letter followed by the network share path).
  • Persistent Mappings: The /persistent:yes flag keeps drives mapped after reboot—change this to /persistent:no if you want temporary mappings.
  • Admin Permissions: Depending on your network setup, you may need to run the generated AutoMapDrives.bat as an administrator to delete or remap drives.
  • Space Handling: The script automatically quotes network paths, so shares with spaces in their names will work correctly.

How to Use

  1. Place this script in the same folder as your mapped_drives.txt
  2. Double-click to run it
  3. Use the generated AutoMapDrives.bat to remap drives—you can distribute it to users or add it to their startup folder for automatic execution

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.20 08:12:51