@echo off
mode 75,40
cd /d "%~dp0"
setlocal enabledelayedexpansion
set vola=C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z
set dd=%date:~5,2%%date:~8,2%
if %time:~0,2% leq 9 (set hh=0%time:~1,1%) else (set hh=%time:~0,2%)
set mm=%time:~3,2%
set ss=%time:~6,2%
call :setESC
:menu
echo.                      System Command Dism Tools
echo.
echo.                           1  系统   安装
echo.
echo.                           2  系统   备份
echo.
echo.                           3  系统   还原
echo.  
echo.                           4  镜像   挂载
echo.
echo.                           5  镜像   卸载
echo.
echo.                           6  WIM ^<-^> ESD
echo.
echo.                           7  WPE ^<-^> BOOT
echo.
echo.                           [Quit]^=^=^>^> EXIT
echo.
set /p  id=选择项目序号: 
if "%id%"=="1" goto install
if "%id%"=="2" goto backup
if "%id%"=="3" goto restore
if "%id%"=="4" goto mountimage
if "%id%"=="5" goto unmount
if "%id%"=="6" goto wtoe
if "%id%"=="7" goto pezip
if /i "%id%"=="quit" exit
cls
goto menu

:install
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:installerr1
set vid=
set /p vid=选择将要安装系统的驱动器: 
if /i "%vid%"=="quit" cls&goto menu
if not exist %vid%: goto installerr1
for %%i in (%vola%) do (if /i %vid%==%%i set vid=%%i)
set vid=%vid%:
echo.
:installerr2
set fid=
set /p fid=选择存放安装文件的驱动器: 
if /i "%fid%"=="quit" cls&goto menu 
if not exist %fid%: goto installerr2
for %%i in (%vola%) do (if /i %fid%==%%i set fid=%%i)
set fid=%fid%:
echo.
set n=
cd /d %fid%\ &for /f %%i in ('dir /b /s *.wim,*.esd') do (set /a n+=1 &echo   !n! ^<^=^= %%i&set a!n!=%%i)
echo.
if not defined n goto installerr2
:installerr3
set fid=
set /p fid=选择文件序号: 
if /i "%fid%"=="quit" cls&goto menu 
if not defined fid  goto installerr3
set /a fid=fid
if %fid% leq 0 goto installerr3
if %fid% gtr %n% goto installerr3
If %fid% leq %n%  set fid=!a%fid%!
echo.
set n=
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find /i "dism.log"') do (
echo.%ESC%[31m  %fid% [文件损坏或格式未知]  %ESC%[0m &echo.&pause&cls&goto menu)
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "Name"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "名称"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)
echo.
:installerr4
set index=
set /p index=选择索引序号: 
if /i "%index%"=="quit" cls&goto menu  
if not defined index  goto installerr4
set /a index=index
if %index% leq 0 goto installerr4
if %index% gtr %n% goto installerr4
echo.
set fmid=
set /p fmid= %ESC%[31m  是否格式化安装系统的驱动器[%vid%]: 是(Y) 否(ANY) %ESC%[0m 
if not defined fmid  set fmid=n
echo.
if /i %fmid%==y format %vid% /y /q /x /fs:ntfs
echo.
echo.%ESC%[92m  %fid% ^=^=^> %vid%  %ESC%[0m
echo.
dism.exe /apply-image  /imagefile:%fid% /index:%index% /applydir:%vid%\
echo.
echo.[=======================正在优化启动=======================]
echo.
dism.exe /Image:%vid%\ /Optimize-Image /Boot
echo.
bootsect.exe /nt60 %vid% /mbr
if exist %vid%\Boot\BCD del /q %vid%\Boot\BCD
if exist %vid%\EFI\Microsoft\Boot\BCD del /q %vid%\EFI\Microsoft\Boot\BCD
bcdboot.exe %vid%\Windows /s %vid% /l zh-CN /f ALL
REG.EXE LOAD HKLM\TempHive %vid%\Windows\System32\config\software
for /f "tokens=3,*" %%i in ('reg query "HKLM\TempHive\Microsoft\Windows NT\CurrentVersion"  /v  ProductName^|find "ProductName"') do set des=%%i %%j
REG.EXE UNLOAD HKLM\TempHive
if defined des bcdedit.exe -store %vid%\Boot\BCD -set {default} description "%des%"
if defined des bcdedit.exe -store %vid%\EFI\Microsoft\Boot\BCD -set {default} description "%des%"
echo.
echo 安装结束
echo.
pause&cls&goto menu

:backup
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:backerr1
set vid=
set /p vid=选择将要备份的驱动器: 
if /i "%vid%"=="quit" cls&goto menu 
if not exist %vid%: goto backerr1
for %%i in (%vola%) do (if /i %vid%==%%i set vid=%%i)
set vid=%vid%:
echo.
:backerr2
set fid=
set /p fid=选择存放文件的驱动器: 
if /i "%fid%"=="quit" cls&goto menu 
if not exist %fid%: goto backerr2
for %%i in (%vola%) do (if /i %fid%==%%i set fid=%%i)
set fid=%fid%:
echo.
echo. %ESC%[92m  %vid% ^=^=^> %fid%\Sys_back\Sys_back_%dd%%hh%%mm%.wim %ESC%[0m
if not exist %fid%\Sys_back md %fid%\Sys_back
dism.exe /Capture-Image /ImageFile:%fid%\Sys_back\Sys_back_%dd%%hh%%mm%.wim ^
/CaptureDir:%vid% /Name:Sys_back /Compress:max /CheckIntegrity /Verify /ScratchDir:%fid%\
echo.
echo 备份结束.
echo.
pause&cls&goto menu

:restore
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:restorerr1
set vid=
set /p vid=选择需要还原的驱动器: 
if /i "%vid%"=="quit" cls&goto menu  
if not exist %vid%: goto restorerr1
for %%i in (%vola%) do (if /i %vid%==%%i set vid=%%i)
set vid=%vid%:
echo.
set n=
for %%i in (%vola%) do (if exist %%i:\Sys_back for %%i in (%%i:\Sys_back\*.wim,%%i:\Sys_back\*.esd) do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i))
if not defined n echo 没找到还原文件! 按任意键继续 .&pause>nul &cls&goto menu
echo.
:restorerr2
set fid=
set /p fid=选择存放文件的驱动器: 
if /i "%fid%"=="quit" cls&goto menu 
echo.
if not defined fid  goto restorerr2
set /a fid=fid
if %fid% leq 0 goto restorerr2
if %fid% gtr %n% goto restorerr2
If %fid% leq %n% set fid=!a%fid%!
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find /i "dism.log"') do (
echo.%ESC%[31m  %fid% [文件损坏或格式未知]  %ESC%[0m &echo.&pause&echo. &cls&goto menu)
set fmid=
set /p fmid=%ESC%[31m  是否格式化目标磁盘[%vid%]: 是(Y) 否(ANY)  %ESC%[0m 
if not defined fmid  set fmid=n
echo.
if /i %fmid%==y format %vid% /y /q /x /fs:ntfs
echo.
echo.%ESC%[92m  %fid% ^=^=^> %vid%  %ESC%[0m
dism.exe /apply-image  /imagefile:%fid% /index:1 /applydir:%vid%\
bootsect.exe /nt60 %vid% /mbr
echo.
echo 还原结束
echo.
pause&cls&goto menu

:mountimage
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:mountimageerr1
set vid=
set /p vid=选择挂载到那个驱动器: 
if /i "%vid%"=="quit" cls&goto menu 
if not exist %vid%: goto mountimageerr1
for %%i in (%vola%) do (if /i %vid%==%%i set vid=%%i)
set vid=%vid%:
echo.
:mountimageerr2
set fid=
set /p fid=选择存放文件的驱动器: 
if /i "%fid%"=="quit" cls&goto menu 
if not exist %fid%: goto mountimageerr2
for %%i in (%vola%) do (if /i %fid%==%%i set fid=%%i)
set fid=%fid%:
echo.
set n=
cd /d %fid%\ &for /f %%i in ('dir /b /s *.wim') do (set /a n+=1 &echo   !n! ^<^=^= %%i&set a!n!=%%i)
echo.
if not defined n goto mountimageerr2
:mountimageerr3
set fid=
set /p fid=选择文件序号: 
if /i "%fid%"=="quit" cls&goto menu 
if not defined fid  goto mountimageerr3
set /a fid=fid
if %fid% leq 0 goto mountimageerr3
if %fid% gtr %n% goto mountimageerr3
If %fid% leq %n%  set fid=!a%fid%!
echo.
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find /i "dism.log"') do (
echo.%ESC%[31m  %fid% [文件损坏或格式未知]  %ESC%[0m &echo.&pause&echo. &cls&goto menu)
set n=
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "名称"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "Name"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)
echo.
:mountimageerr4
set index=
set /p index=选择索引序号: 
if /i "%index%"=="quit" cls&goto menu 
if not defined index  goto mountimageerr4
set /a index=index
if %index% leq 0 goto mountimageerr4
if %index% gtr %n% goto mountimageerr4
set vvid=%vid%\MountTmp%hh%%mm%%ss%
if not exist %vvid% md %vvid%
echo.
echo.%ESC%[92m  %fid% ^=^=^> %vvid%  %ESC%[0m
dism.exe /Mount-Image /ImageFile:%fid% /Index:%index% /MountDir:%vvid%
echo.
echo 挂载结束
echo.
pause&cls&goto menu

:unmount
echo.
set n=
::for %%i in (%vola%) do (if exist %%i:\MountTmp* set /a n+=1 &echo   !n! : %%i:\MountTmp*&set a!n!=%%i:\MountTmp*)
for %%i in (%vola%) do if exist %%i:\MountTmp* for /f %%i in ('dir /b/s %%i:\MountTmp*') do (set /a n+=1 &echo   !n! ^<^=^= %%i&set a!n!=%%i)
if not defined n echo 没找到挂载点! 按任意键继续 .&pause>nul &cls&goto menu
:unmounterr1
echo.
set fid=
set /p fid=选择将要卸载的挂载点: 
if /i "%fid%"=="quit" cls&goto menu 
if not defined fid  goto unmounterr1
set /a fid=fid
if %fid% leq 0 goto unmounterr1
if %fid% gtr %n% goto unmounterr1
If %fid% leq %n%  set fid=!a%fid%!
echo.
echo.%ESC%[92m  %fid% ^=^=^> ^NUL  %ESC%[0m
echo.
set umid=
set /p umid=%ESC%[31m  是否保存修改: 是(Y) 否(ANY) %ESC%[0m
if not defined umid  set umid=n
if /i %umid%==y dism.exe /unmount-wim /mountdir:%fid% /commit&rd/q %fid%&echo.&echo 卸载结束&echo.&pause&cls&goto menu
dism.exe /unmount-wim /mountdir:%fid% /discard&rd/q %fid%
echo.
echo 卸载结束
echo.
pause&cls&goto menu

:wtoe
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:wtoeerr1
set fid=
set /p fid=转换文件存放的驱动器: 
if /i "%fid%"=="quit" cls&goto menu  
if not exist %fid%: goto wtoeerr1
for %%i in (%vola%) do (if /i %fid%==%%i set fid=%%i)
set fid=%fid%:
echo.
set n=
cd /d %fid%\ &for /f %%i in ('dir /b /s *.wim,*.esd') do (set /a n+=1 &echo   !n! ^<^=^= %%i&set a!n!=%%i)
echo.
if not defined n goto wtoeerr1
:wtoeerr2
set fid=
set /p fid=选择文件序号: 
if /i "%fid%"=="quit" cls&goto menu 
if not defined fid  goto wtoeerr2
set /a fid=fid
if %fid% leq 0 goto wtoeerr2
if %fid% gtr %n% goto wtoeerr2
If %fid% leq %n%  set fid=!a%fid%!
echo.
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find /i "dism.log"') do (
echo.%ESC%[31m  %fid% [文件损坏或格式未知]  %ESC%[0m &echo.&pause&echo. &cls&goto menu)
set n=
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "名称"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find "Name"') do (set /a n+=1 &echo   !n! : %%i&set a!n!=%%i)   
echo.
:wtoeerr3
set index=
set /p index=选择索引序号: 
if /i "%index%"=="quit" cls&goto menu 
if not defined index  goto wtoeerr3
set /a index=index
if %index% leq 0 goto wtoeerr3
if %index% gtr %n% goto wtoeerr3
echo.

if  /i %fid:~-3%==wim echo.%ESC%[92m  %fid% ^=^=^>^> %fid:~-0,-3%esd  %ESC%[0m
if  /i %fid:~-3%==wim dism.exe /Export-Image /SourceImageFile:%fid% /SourceIndex:%index% /DestinationImageFile:%fid:~-0,-3%esd /Compress:recovery /CheckIntegrity
if  /i %fid:~-3%==esd echo.%ESC%[92m  %fid% ^=^=^>^> %fid:~-0,-3%wim  %ESC%[0m
if  /i %fid:~-3%==esd  dism.exe /Export-Image /SourceImageFile:%fid% /SourceIndex:%index% /DestinationImageFile:%fid:~-0,-3%wim /Compress:max /CheckIntegrity
echo.
echo 转换结束
echo.
pause&cls&goto menu

:pezip
echo.
set n=
for %%i in (%vola%) do (if exist %%i: set /a n+=1 &echo   !n! ^=^=^> %%i:)
echo.
:peziperr1
set vid=
set /p vid=Boot.wim、PEboot在那个驱动器: 
if /i "%vid%"=="quit" cls&goto menu 
if not exist %vid%: goto peziperr1
for %%i in (%vola%) do (if /i %vid%==%%i set vid=%%i)
set vid=%vid%:
echo.
set n=
cd /d %vid%\ &for /f %%i in ('dir /b /s boot.wim,PEboot*') do (set /a n+=1 &echo   !n! ^<^=^= %%i&set a!n!=%%i)
echo.
if not defined n echo 没找到Boot.wim、PEboot 按任意键继续 .&pause>nul &echo.&goto peziperr1
echo.
:peziperr2
set fid=
set /p fid=释放文件、打包文件夹: 
if /i "%fid%"=="quit" cls&goto menu 
if not defined fid  goto peziperr2
set /a fid=fid
if %fid% leq 0 goto peziperr2
if %fid% gtr %n% goto peziperr2
If %fid% leq %n%  set fid=!a%fid%!
if /i %fid:~-3,3%==wim goto uwim
set did=%fid:~-0,-14%%fid:~-8,8%
if not exist %did% md %did% 
if exist %did%\boot.wim del %did%\boot.wim  
echo.
echo.%ESC%[92m  %fid% ^=^=^> %did%\boot.wim  %ESC%[0m
echo.
dism.exe /Capture-Image /ImageFile:%did%\boot.wim /CaptureDir:%fid% /Name:WinPE /Compress:max /Bootable /CheckIntegrity /Verify /ScratchDir:%vid%\
echo.
echo 打包结束
echo.
pause&cls&goto menu
:uwim
echo.
for /f "tokens=2 delims=:" %%i in ('dism.exe /get-wiminfo /wimfile:%fid% ^|find /i "dism.log"') do (
echo.%ESC%[31m  %fid% [文件损坏或格式未知]  %ESC%[0m &echo.&pause&echo. &cls&goto menu)
set ffid=%fid:~-0,-8%PEboot%dd%%hh%%mm%%
if not exist %ffid% md %ffid%
echo.
echo.%ESC%[92m  %fid% ^=^=^> %ffid%  %ESC%[0m
echo.
dism.exe /apply-image  /imagefile:%fid% /index:1 /applydir:%ffid%\
echo.
echo 释放结束
echo.
pause&cls&goto menu

:setESC
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do rem"') do (
  set ESC=%%b
  exit /B 0
)
exit /B 0

::======木化石与2022年12月16日 完成于齐齐哈尔======