编写可读取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.txtexists to avoid errors - Skip empty lines in the input file to prevent invalid commands
- Generate
net usecommands 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.txtmust have one mapping per line, formatted likeZ: \\server01\user_documents(drive letter followed by the network share path). - Persistent Mappings: The
/persistent:yesflag keeps drives mapped after reboot—change this to/persistent:noif you want temporary mappings. - Admin Permissions: Depending on your network setup, you may need to run the generated
AutoMapDrives.batas 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
- Place this script in the same folder as your
mapped_drives.txt - Double-click to run it
- Use the generated
AutoMapDrives.batto remap drives—you can distribute it to users or add it to their startup folder for automatic execution
内容的提问来源于stack exchange,提问作者supeg
相关产品推荐
相关产品推荐

