commit_wim.cmd 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. @echo off
  2. chcp 65001
  3. ::cls
  4. ::color A
  5. setlocal EnableExtensions
  6. setlocal ENABLEDELAYEDEXPANSION
  7. set LOGFILE="%~dp0dism.log"
  8. set DISM_CMD="%WINDIR%\SysWOW64\DISM.exe"
  9. set "DISM_KEYS=/Quiet /NoRestart /LogPath:%LOGFILE%"
  10. set SRCWIM=%~1
  11. if not defined SRCWIM call :PRINT_USAGE
  12. set SRCUPD=%~2
  13. if not defined SRCUPD call :PRINT_USAGE
  14. set DESTPATH=%~3
  15. if not defined DESTPATH call :PRINT_USAGE
  16. if not exist "%DESTPATH%" ( md "%DESTPATH%" || exit /b )
  17. set PROCS="%~dp0procs.cmd"
  18. if /i "%SRCUPD%" equ "%DESTPATH%" ( echo ERROR: Source and destination are the same! %SRCUPD% >&2 && exit /b )
  19. del /q /s "%LOGFILE%" 2>NUL
  20. %DISM_CMD% %DISM_KEYS% /mount-wim /wimfile:"%SRCWIM%" /index:1 /mountdir:"%DESTPATH%"
  21. if errorlevel 1 (echo ERROR: Dism failed. Code: !ERRORLEVEL! >&2)
  22. call :PROCDIR "%SRCUPD%" "\.*.cab$ \.*.msu$" "ADD_PACKAGE"
  23. call :PREMISSION "Unmount image?" ":UNMOUNT"
  24. if exist %LOGFILE% call :PREMISSION "Type %LOGFILE%?" "cls && type %LOGFILE%"
  25. echo.&pause&goto:eof
  26. :UNMOUNT
  27. call :PREMISSION "Commit image?" ":COMMIT" ":DISCARD"
  28. call :CLEANUP
  29. exit /b !ERRORLEVEL!
  30. :PREMISSION
  31. if [%1] == [] call :ERROR_ARG %*
  32. setlocal
  33. set QUESTION=%~1
  34. set TRUEPROC=%~2
  35. set FALSEPROC=%~3
  36. set /P answer=%QUESTION% ^(Y^)^:
  37. if /i "%answer:~,1%" EQU "Y" (
  38. call %TRUEPROC%
  39. ) else (
  40. call %FALSEPROC%
  41. )
  42. endlocal
  43. exit /b !ERRORLEVEL!
  44. :COMMIT
  45. start "" /b /w %DISM_CMD% %DISM_KEYS% /Unmount-Wim /MountDir:%DESTPATH% /commit
  46. if !ERRORLEVEL! NEQ 0 (
  47. @echo.Failed. Error:!errorlevel!
  48. ) else (
  49. echo WIM Updated with Changes.
  50. )
  51. echo.
  52. exit /b !ERRORLEVEL!
  53. :DISCARD
  54. start "" /b /w %DISM_CMD% %DISM_KEYS% /Unmount-Wim /MountDir:%DESTPATH% /discard
  55. if !ERRORLEVEL! NEQ 0 (
  56. @echo.Failed. Error:!errorlevel!
  57. ) else (
  58. echo WIM NOT updated, changes discarded.
  59. echo.
  60. exit /b !ERRORLEVEL!
  61. :CLEANUP
  62. %DISM_CMD% %DISM_KEYS% /CLEANUP-WIM && echo %DESTPATH% clean up finished.
  63. echo.
  64. exit /b !ERRORLEVEL!
  65. :PROCDIR
  66. if [%1] == [] call :ERROR_ARG %*
  67. setlocal
  68. set SRCDIR=%~1
  69. set FILEMASKS=%~2
  70. if not defined FILEMASKS set FILEMASKS="\.*"
  71. set PROCNAME=%~3
  72. set TOTAL=0
  73. set COUNTER=0
  74. for /f "usebackq delims=" %%I in ( `dir /b /s /a:-d "%SRCDIR%\*" 2^>NUL ^| findstr /i "%FILEMASKS%" 2^>NUL` ) do (
  75. set /a TOTAL+=1
  76. )
  77. for /f "usebackq delims=" %%I in ( `dir /b /s /a:-d "%SRCDIR%\*" 2^>NUL ^| findstr /i "%FILEMASKS%" 2^>NUL` ) do (
  78. call :%PROCNAME% "%%~I"
  79. set /a COUNTER+=1
  80. set /a PTS= 100*!COUNTER!/!TOTAL!
  81. @echo.!COUNTER!^/!TOTAL! !PTS!^%% done.
  82. @echo.
  83. )
  84. endlocal
  85. exit /b !ERRORLEVEL!
  86. :ADD_PACKAGE
  87. if [%1] == [] call %PROCS% ERROR_ARG %*
  88. setlocal
  89. set FILENAME=%~1
  90. echo Injecting "%FILENAME%"
  91. %DISM_CMD% %DISM_KEYS% /image:"%DESTPATH%" /add-package /packagepath:"%FILENAME%"
  92. if errorlevel 1 (echo ERROR: Dism failed. Code: !ERRORLEVEL! >&2)
  93. endlocal
  94. exit /b !ERRORLEVEL!
  95. :PRINT_USAGE
  96. echo:
  97. echo USAGE:
  98. echo %~n0 ^<source^> ^<update_source^> ^<destination^>
  99. exit /b !ERRORLEVEL!