::准备全局变量
set /a a=0
set /a OpChoice=0
set /a NameUnit=0
set /a CountWim=0
set /a CountIndex=0
set /a NumWimSource=0
set NameWimSource=null
set /a IndexSource=0
set /a NumWimDest=0
set NameWimDest=null
::准备全局临时文件夹\tmp_Imagex,预备以后作为ScratchDir使用
if not exist %~dp0tmp_Imagex goto mdTmp
call :rd_Pre %~dp0tmp_Imagex
:mdTmp
md %~dp0tmp_Imagex
call :ChoiceIn 4
set /a OpChoice=!Op!
if !OpChoice!==1 (
echo 任务类型 1: 系统封装与功能调整。
::准备局部变量
set /a CountPatch=0
set /a NumPatch=0
set NameDirPat=null
set /a CountDrv=0
set /a NumDrv=0
set NameDirDrv=null
::准备局部文件夹\mount
if not exist %~dp0mount goto mdmount
call :rd_Pre %~dp0mount
:mdmount
md %~dp0mount
set /p bPatDism=请问是否封装系统补丁?默认是y,直接回车/不是输入n----
set /p bDrvDism=请问是否封装硬件驱动?默认是y,直接回车/不是输入n----
set /p bIsWin8=请问是要操作Win8映像吗?默认是y,直接回车/不是输入n----
if not !bIsWin8!==n (
set /p bEnabNet3=请问是否启用 .Net 3.5/2支持?默认是y,直接回车/不是输入n----
if not !bEnabNet3!==n (
set /p LabelISO=请插入光盘或者装载ISO,并准确输入盘符!----
)
)
if !bIsWin8!==n (
set /p bDisabFun=请问是否关闭Win7部分功能?默认是y,直接回车/不是输入n----
set /p bDismIE10=请问是否封装IE10?默认是y,直接回车/不是输入n----
)
::准备源系统映像文件与索引序号。
call :SeekWim
set /a CountWim=!a!
echo 下面进行系统文件序号选择
call :ChoiceIn !CountWim!
set /a NumWimSource=!Op!
call :GetNameUnit !NumWimSource! %temp%\fSeek.txt
set NameWimSource=!NameUnit!
echo 下面进行映像索引序号选择
call :SeekCIndex !NameWimSource!
call :ChoiceIn !CountIndex!
set /a IndexSource=!Op!
if not !bPatDism!==n (
call :dirChild %~dp0patch
set /a CountPatch=!a!
echo 下面进行补丁文件夹序号选择
call :ChoiceIn !CountPatch!
set /a NumPatch=!Op!
call :GetNameUnit !NumPatch! %temp%\dirSeek.txt
set NameDirPat=!NameUnit!
dir %~dp0patch\!NameDirPat! /b /s >%temp%\fSeek.txt
)
if not !bDrvDism!==n (
call :dirChild %~dp0driver
set /a CountDrv=!a!
echo 下面进行驱动文件夹序号选择
call :ChoiceIn !CountDrv!
set /a NumDrv=!Op!
call :GetNameUnit !NumDrv! %temp%\dirSeek.txt
set NameDirDrv=!NameUnit!
)
dism /mount-image /imagefile:!NameWimSource! /index:!IndexSource! /mountdir:%~dp0mount
if not !bPatDism!==n ( for /f "usebackq delims=" %%i in (%temp%\fSeek.txt) do ( dism /image:%~dp0mount /add-package /packagepath:%%i ) )
if not !bDrvDism!==n dism /image:%~dp0mount /add-driver /driver:%~dp0Driver\!NameDirDrv! /recurse /forceunsigned
if !OpChoice!==2 (
echo 任务: 将Wim文件中的映像应用于某一分区,即装系统,未提供完整引导重建。
echo UEFI模式下可以建立Win8.1/8/7 x64引导,就是完整系统安装了。
echo -------------------------------------------------------------------------------
set /p LabelApply=请输入要安装系统的盘符,请现在自行格式化,不带冒号----
set /p bBootNeed=是否同时建立系统引导,必须是UEFI模式Win8.1/8/7 x64,默认不n,直接回车/是输入y----
if !bBootNeed!==y (
echo list vol>%temp%\DiskPart.txt
echo exit>>%temp%\DiskPart.txt
diskpart /s %temp%\DiskPart.txt
set /p bLabelHave=请问ESP分区是否已经有盘符,默认是y,直接回车/不是输入n---
if !bLabelHave!==y (
set /p Label_ESP=请输入ESP分区现在的盘符,不带冒号,我不检测无效盘符噢----
goto validLESP)
set /p choiceVol_6=请输入ESP分区的卷编号,直接回车----
set /p Label_ESP=请输入要指定的盘符,不带冒号,我不检测无效盘符噢----
echo select vol !choiceVol_6!>%temp%\DiskPart.txt
echo assign letter=!Label_ESP!>>%temp%\DiskPart.txt
echo exit>>%temp%\DiskPart.txt
diskpart /s %temp%\DiskPart.txt
)
:validLESP
::准备源系统映像文件与索引序号。
call :SeekWim
set /a CountWim=!a!
echo 下面进行系统文件序号选择
call :ChoiceIn !CountWim!
set /a NumWimSource=!Op!
call :GetNameUnit !NumWimSource! %temp%\fSeek.txt
set NameWimSource=!NameUnit!
echo 下面进行映像索引序号选择
call :SeekCIndex !NameWimSource!
call :ChoiceIn !CountIndex!
set /a IndexSource=!Op!
dism /Apply-Image /ImageFile:!NameWimSource! /Index:!IndexSource! /ApplyDir:!LabelApply!:\
if !bBootNeed!==y (
if exist !LabelApply!:\windows\system32\bcdboot.exe !LabelApply!:\windows\system32\bcdboot !LabelApply!:\windows /s !Label_ESP!: /f ALL
)
goto Proceed
)
if !OpChoice!==3 (
echo 任务类型 3: 将某一分区系统捕获为系统映像文件,即wim文件。
set /p LabelCapture=请输入要捕获系统的盘符,不带冒号----
set /p NameInput=请输入要捕获制作映像的名字,接收数字与字母,不要奇怪的字符----
:: 格式化映像名字,以下划线代替空格
set /a a=0
for %%i in ( !NameInput! ) do (
set /a a+=1
if !a!==1 set NameImage=%%i
if not !a!==1 set NameImage=!NameImage!_%%i
)
set /p bCaptureAppend=是否增量捕获在其它映像文件中,默认是y,直接回车/不是输入n----
if not !bCaptureAppend!==n (
call :SeekWim
set /a CountWim=!a!
echo 下面进行系统文件序号选择
call :ChoiceIn !CountWim!
set /a NumWimDest=!Op!
call :GetNameUnit !NumWimDest! %temp%\fSeek.txt
set NameWimDest=!NameUnit!
echo !NameWimDest!
dism /Append-Image /ImageFile:!NameWimDest! /CaptureDir:!LabelCapture!:\ /Name:!NameImage!
goto Proceed
)
dism /Capture-Image /ImageFile:%~dp0!NameImage!.wim /CaptureDir:!LabelCapture!:\ /Name:!NameImage!
goto Proceed
)
if !OpChoice!==4 (
echo 任务类型 4: Wim文件导入导出整理。
::准备源系统映像文件与索引序号。
call :SeekWim
set /a CountWim=!a!
echo 下面进行系统文件序号选择
call :ChoiceIn !CountWim!
set /a NumWimSource=!Op!
call :GetNameUnit !NumWimSource! %temp%\fSeek.txt
set NameWimSource=!NameUnit!
echo 下面进行映像索引序号选择
call :SeekCIndex !NameWimSource!
call :ChoiceIn !CountIndex!
set /a IndexSource=!Op!
find "名称" %temp%\imageinfo.txt>%temp%\tmp.txt
set /a a=0
for /f "skip=2 tokens=2 delims= " %%i in (%temp%\tmp.txt) do (
set /a a+=1
if !IndexSource!==!a! set NameTmp=%%i
)
:: 格式化映像名字,以下划线代替空格
set /a a=0
for %%i in ( !NameTmp! ) do (
set /a a+=1
if !a!==1 set NameImage=%%i
if not !a!==1 set NameImage=!NameImage!_%%i
)
set /p bCaptureAppend=是否导出在其它映像文件中,默认是y,直接回车/不是输入n----
if not !bCaptureAppend!==n (
call :SeekWim
set /a CountWim=!a!
echo 下面进行系统文件序号选择
call :ChoiceIn !CountWim!
set /a NumWimDest=!Op!
call :GetNameUnit !NumWimDest! %temp%\fSeek.txt
set NameWimDest=!NameUnit!
dism /Export-Image /SourceImageFile:!NameWimSource! /SourceIndex:!IndexSource! /DestinationImageFile:!NameWimDest!
goto Proceed
)
dism /Export-Image /SourceImageFile:!NameWimSource! /SourceIndex:!IndexSource! /DestinationImageFile:%~dp0!NameImage!.wim
goto Proceed
)
:Proceed
set IfProceed=n
set /p IfProceed=请问继续其它任务吗,第二次运行默认应答选项无效,默认不n,直接回车/是输入y----
if !IfProceed!==y goto opInput
call :rd_Pre %~dp0tmp_Imagex
call :rd_Pre %~dp0mount
del /f /s /q %temp%\fSeek.txt
del /f /s /q %temp%\ImageInfo.txt
del /f /s /q %temp%\tmp.txt
del /f /s /q %temp%\del_log.txt
del /f /s /q %temp%\del_rd.txt
del /f /s /q %temp%\dirSeek.txt
del /f /s /q %temp%\DiskPart.txt
echo 任务结束时间是%time:~,2%点%time:~3,2%分%time:~6,2%秒
set "T2=%time%"
if "%T2:~,2%" lss "%T1:~,2%" (set Tday+=8640000)
set /a "T_d=%Tday%+(%T2:~,2%*360000)+(%T2:~3,1%*60000+%T2:~4,1%*6000)+(%T2:~6,1%*1000+%T2:~7,1%*100)+(%T2:~9,1%*10+%T2:~-1%)-(%T1:~,2%*360000)-(%T1:~3,1%*60000+%T1:~4,1%*6000)-(%T1:~6,1%*1000+%T1:~7,1%*100)-(%T1:~9,1%*10+%T1:~-1%)"
set /a "ss=%T_d%/100%%60"
set /a "mm=%T_d%/6000%%60"
set /a "hh=%T_d%/360000%%1"
set /a "ms=%T_d%%%100"
echo 本次任务消耗时间为 %hh%:%mm%:%ss%.%ms%秒
pause
exit
:: 程序段_1:清理某文件夹,需传入参数%1,完整目录名,示例:call :rd_Pre %~dp0mount
:rd_Pre
:rd
takeown /f %1 /r /d y >%temp%\del_log.txt>nul 2>nul
@echo y|icacls %1 /t /q /grant administrator:f>nul 2>nul
rd %1 /s /q
if not exist %1 goto :eof
dir %1 /d /b >%temp%\del_rd.txt
for /f "usebackq delims=" %%i in (%temp%\del_rd.txt) do ((takeown /f "%%i" /r /d y >%temp%\del_log.txt>nul 2>nul)&(@echo y|icacls "%%i" /t /q /grant administrator:f>nul 2>nul )&(rd "%%i" /s /q>nul 2>nul))
if exist %1 goto :rd
:: 程序段_2:查找当前目录下所有映像文件wim,不需传入参数,示例:call :SeekWim
:: 注意:及时取出统计变量a的值,文件完整路径加名字信息在fSeek.txt中
:SeekWim
cls
set /a a=0
dir %~dp0*.wim /b /s >%temp%\fSeek.txt
echo 查找到%~dp0目录下所有映像文件如下表
echo -----------------------------------------------------
for /f "usebackq delims=" %%i in (%temp%\fSeek.txt) do ( set /a a+=1&&echo 系统映像:!a! %%i)
if !a!==0 echo 无系统映像文件,直接退出
echo -----------------------------------------------------
goto :eof
:: 程序段_3:传入参数:指定完整目录,查找其下所有子目录,示例:call :dirChild %~dp0patch
:: 注意:及时取出统计变量a的值,目录信息在dirSeek.txt中,也最好通过重命名取走
:dirChild
cls
set /a a=0
dir %1 /d /b >%temp%\dirSeek.txt
echo 查找到%1目录下所有子目录,仅查子目录
for /f "usebackq delims=" %%i in (%temp%\dirSeek.txt) do (
set /a a+=1
echo 所有子目录:!a! %%i)
if !a!==0 echo 无无任何子目录
echo -----------------------------------------------------
goto :eof
:: 程序段_4:序号选择,需传入参数:可选范围,示例:call :ChoiceIn !CountPatch!
:: 注意:用户序号输入的值已经放在变量op了,请及时取值使用。
:ChoiceIn
:op
echo ------------------------------------------------------------------------------
set /p op=请输入选择序号:1-%1----
for /l %%i in (1,1,%1) do (
if !op!==%%i goto VldInput
)
echo 这么简单的选择题都会错,输入数字1-%1有这么难吗,请重新输入
echo ------------------------------------------------------------------------------
goto op
:VldInput
echo -----------------------------------------------------
goto :eof
:: 程序段_5:根据指定序号(参数%1)取出文件(参数%2)中指定行内容
:: 注意:指定行内容在变量NameUnit中,请及时取值使用。
:GetNameUnit
set /a a=0
for /f %%i in ( %2 ) do (
set /a a+=1
if !a!==%1 set NameUnit=%%i
)
goto :eof