@echo off
setlocal enabledelayedexpansion
title (快速安装VHD系统 By baby1277 2025.2.26)
:: 检查是否有拖拽文件输入
set "wim=%~1"
if defined wim goto wimok
:vhdset
cls
echo.
echo 请将Wim或Esd映像文件拖拽进来后按回车键
echo.
set /p "wim=请输入文件路径: "
if "!wim!"=="" goto vhdset
:wimok
:: 验证文件扩展名和存在性
for %%i in ("%wim%") do (
if /i not "%%~xi"==".wim" if /i not "%%~xi"==".esd" (
echo 错误:仅支持.wim或.esd文件
goto vhdset
)
if not exist "%%~fi" (
echo 错误:文件不存在
goto vhdset
)
set "wim2=%%~fi"
)
::获取映像索引...
::Dism.exe /get-wiminfo /wimfile:"!wim2!" | more
call :ChoiseIndex !wim2!
:: 解析名称
for /f "tokens=1-6" %%i in ('Dism.exe /get-wiminfo /wimfile:"%wim2%" /index:!index!') do (
if "%%i"=="名称:" set mod=%%j&set num=%%k&set ver0=%%l&set ver1=%%m&set ver2=%%n
)
set "vhd_title=%mod% %num% %ver0% %ver1% %ver2%"
:: 获取版本信息
for %%v in ("旗舰版=Ultimate" "企业版=Enterprise" "专业版=Professional") do (
set "pair=%%~v"
if "!ver0!"=="!pair:~0,3!" set "ver0=!pair:~4!"
if "!ver1!"=="!pair:~0,3!" set "ver0=!pair:~4!"
)
:: 设置VHD类型
set "nx=.vhdx" & set "mb=gpt"
if %num% leq 7 set "nx=.vhd" & set "mb=mbr"
:: 用户输入VHD位置
:input_drive
echo.
set /p "vhd_d0=请输入存放VHD的盘符(如 C D E): "
if not exist "!vhd_d0!:\" (
echo 错误:盘符无效
goto input_drive
)
:: 生成唯一文件名
set "vhd_fn=!mod:~0,3!!num!!ver0:~0,3!"
set "vhd_dir=!vhd_d0!:\!vhd_fn!"
set "vhd_path=!vhd_dir!\!vhd_fn!!nx!"
if exist "!vhd_path!" (
set "cnt=1"
:name_check
set "vhd_path=!vhd_dir!\!vhd_fn!_!cnt!!nx!"
if exist "!vhd_path!" (set /a cnt+=1 & goto name_check)
)
:: 创建目录
if not exist "!vhd_dir!" mkdir "!vhd_dir!"
:: 设置大小
if "%value%"=="" set "value=32"
set /a size=value*1024
cls
echo.
echo.
echo.
echo ===========请确认VHD参数:
echo.
echo VHD磁盘卷标:!vhd_fn!
echo.
echo VHD文件位置:!vhd_path!
echo.
echo VHD文件大小:%value%GB (最大:%size%MB)
echo.
echo Wim/Esd位置:%wim2%
echo.
echo Wim/Esd卷号:%index%
echo.
echo Wim/Esd名称:%vhd_title%
echo.
echo =====按任意键... 创建虚拟磁盘 (!vhd_path!)
pause>nul
echo.&echo.
:: 创建虚拟磁盘
echo 正在创建VHD...
for %%i in (R S T U V W Y Z) do if not exist %%i: set pan=%%i
echo create vdisk file=%vhd_path% maximum=%size% type=expandable > "%temp%\vhd.txt"
echo attach vdisk >> "%temp%\vhd.txt"
echo select vdisk file=%vhd_path% >> "%temp%\vhd.txt"
echo clean >> "%temp%\vhd.txt"
echo convert %mb% >> "%temp%\vhd.txt"
echo create partition primary >> "%temp%\vhd.txt"
echo format fs=ntfs label="%vhd_fn%" quick >> "%temp%\vhd.txt"
echo assign letter=%pan%: >> "%temp%\vhd.txt"
diskpart /s "%temp%\vhd.txt"
del "%temp%\vhd.txt" >nul 2>&1
echo.&echo.
if exist %pan%: echo 虚拟磁盘已成功创建并挂载为%pan%:盘 ||echo 创建失败!
pause
:ChoiseIndex
set maxIndex=1
for /f "tokens=2 eol=; delims=: " %%i in ('Dism.exe /Get-ImageInfo /ImageFile:!wim2!') do (
for %%a in (1 2) do (
if "%%i"=="%%a" set maxIndex=%%i
)
)
if not "!maxIndex!"=="1" (
@Dism.exe /Get-WimInfo /WimFile:!wim2!
@echo.
set /p index=请选择Wim映像的索引:
) else (
set index=1
)
goto :eof