找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 6400|回复: 6

[文本处理] [已解决]批处理diskpart下如何选择其他盘符还原镜像

[复制链接]
发表于 2025-5-8 20:45:44 | 显示全部楼层 |阅读模式
  1. @echo off
  2. @rem  This script is called by x:\Windows\System32\Startnet.cmd - WinPE
  3. @echo.
  4. @echo.
  5. @echo.
  6. @echo Running Intel Optimization powercfg for improved imaging times
  7. @echo.
  8. @echo call powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
  9. call powercfg /s 8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c
  10. @echo.
  11. @echo.
  12. @echo.
  13. @rem  *****************************************************************
  14. @rem  List all the disks, then search the volumes of each one to see
  15. @rem  which disks may have "Winpe" in the label. We will ignore that
  16. @rem  disk and use the first disk without the Winpe label that we find.
  17. @rem  *****************************************************************
  18. @echo call diskpart /s %WinPESource%DiskpartFiles\listdisk.txt
  19. call diskpart /s %WinPESource%DiskpartFiles\listdisk.txt>x:\Disks.txt
  20. @echo.
  21. @rem Set Disk equal to blank to start
  22. set disk=
  23. @echo.
  24. @rem
  25. @rem  This line will write a diskpart script to select the disk and detail it's contents.
  26. @rem  We use this output to find a disk that does NOT have Winpe as the volume label.
  27. @rem  That disk will have the OS installed to it. This way you can exclude USB from diskpart.
  28. @rem  
  29. @rem  This works around the issue where the USB key is sometimes enumerated as disk 0 and the
  30. @rem  SSD is disk 1.
  31. @rem  
  32. for /f "skip=8 tokens=2" %%A in (x:\Disks.txt) do (
  33. @echo sel disk %%A>x:\Par.txt
  34. @echo det disk>>x:\Par.txt
  35. @echo exit>>x:\Par.txt
  36. if NOT "%%A"=="DiskPart..." call :ListPar %%A
  37. )
  38. if %Disk%=="" echo No disk found without a volume label of "Winpe"&& set disk=0
  39. @echo Using Disk "%Disk%"
  40. @echo.
  41. @echo.
  42. @echo.
  43. @echo *****************************************************************
  44. @echo  Write the diskpart script now that we know what disk to use
  45. @echo *****************************************************************
  46. @echo.>x:\wimpart.txt
  47. @echo select disk "0">>x:\wimpart.txt
  48. @echo clean>>x:\wimpart.txt
  49. @echo convert gpt>>x:\wimpart.txt
  50. @echo create partition efi size=100>>x:\wimpart.txt
  51. @echo format quick fs=fat32 label="System">>x:\wimpart.txt
  52. @echo assign letter="S">>x:\wimpart.txt
  53. @echo create partition msr size=128>>x:\wimpart.txt
  54. @echo create partition primary>>x:\wimpart.txt
  55. @echo format quick fs=ntfs label="Windows">>x:\wimpart.txt
  56. @echo assign letter="W">>x:\wimpart.txt
  57. @echo exit>>x:\wimpart.txt
  58. @echo.
  59. @echo Wimpart.txt now contains.....
  60. @echo.
  61. type x:\wimpart.txt
  62. @echo.
  63. @echo.
  64. @echo *****************************************************************
  65. @echo  Setting up the partition table
  66. @echo *****************************************************************
  67. @echo call diskpart /s x:\wimpart.txt
  68. diskpart /s x:\wimpart.txt
  69. @echo.
  70. @echo.
  71. @echo.
  72. @echo *****************************************************************
  73. @rem  Map drive letter for Install.wim and Winre.wim
  74. @echo *****************************************************************
  75. @rem Create an output file containing all the drive letters
  76. @echo call diskpart /s %WinPESource%DiskpartFiles\listVol.txt>%WinPESource%Output.txt
  77. diskpart /s %WinPESource%DiskpartFiles\listVol.txt>%WinPESource%Output.txt
  78. @echo.
  79. @rem Go through each drive letter, looking for the images\Install.txt file
  80. for /f "skip=8 tokens=3" %%A in (%WinPESource%Output.txt) do (
  81. if exist %%A:\images\install.txt set InstallPath=%%A:\images&& echo "Found device with images folder and install.txt file"
  82. )
  83. @echo.
  84. @echo.
  85. @rem Check InstallPath was found and there are images in it.
  86. if NOT EXIST %InstallPath%\Install.wim echo "Install.wim NOT found." && goto :Error
  87. @echo Install.wim FOUND at %InstallPath%\images\install.wim
  88. @echo.
  89. @echo ----------------------------------------------------------------------------
  90. @echo ****************************************************************************
  91. @echo  Applying the -Testing- image from the Images Partition in the eMMC
  92. @echo ****************************************************************************
  93. @echo ----------------------------------------------------------------------------
  94. DISM /Apply-Image /ImageFile:"%InstallPath%\install.wim" /ApplyDir:w: /Index:1
  95. if %errorlevel% NEQ 0 echo "Failed to apply image to w:" && goto :error

  96. @echo.
  97. @echo *****************************************************************
  98. @echo  Setting the boot environment
  99. @echo *****************************************************************
  100. @echo call w:\WINDOWS\SYSTEM32\BCDBOOT w:\WINDOWS /s s: /f all
  101. w:\WINDOWS\SYSTEM32\BCDBOOT w:\WINDOWS /s s: /f all
  102. if %errorlevel% NEQ 0 echo "Failed to set boot information" && goto :error

  103. @echo *****************************************************************
  104. @echo  -Testing- image deployment COMPLETE. Type EXIT from Command
  105. @echo  Prompt to restart or turn off device.
  106. @echo *****************************************************************
  107. color 2f
  108. goto :END

  109. :ERROR
  110. @echo.
  111. @echo An error has been detected.
  112. color 4f
  113. @echo.
  114. goto :END

  115. :ListPar
  116. @REM This section is to bring a disk number and use it to List Partitions
  117. @echo.
  118. set TempDisk=%1
  119. diskpart /s x:\Par.txt>x:\ParOutput.txt
  120. for /f "skip=24 tokens=3" %%A in (x:\ParOutput.txt) do (
  121. if /i "%%A"=="no" set Disk=%TempDisk%
  122. )
  123. for /f "skip=26 tokens=4" %%A in (x:\ParOutput.txt) do (
  124. if /i NOT "%%A"=="Winpe" set Disk=%TempDisk%
  125. )
  126. Goto :EOF

  127. :END
  128. @rem
  129. @rem  Get finish time so we can evaluate total WinPE phase time.
  130. @rem
  131. call |time>x:\StopTime.txt
  132. @rem
  133. @rem  Now parse both files to display the Start and Finish Time
  134. @rem
  135. @echo.
  136. @echo.
  137. for /f "tokens=5" %%A in (x:\starttime.txt) do echo Script Start Time is %%A
  138. for /f "tokens=5" %%A in (x:\stoptime.txt) do echo  Script Stop Time is %%A
  139. @echo.
  140. pause
  141. x:\windows\system32\wpeutil shutdown
复制代码
默认的是镜像还原到disk 0,如何改成disk 1,请指教,

评分

参与人数 1PB +2 收起 理由
Batcher + 2 感谢给帖子标题标注[已解决]字样

查看全部评分

发表于 2025-5-8 23:53:09 | 显示全部楼层
  1. @echo *****************************************************************
  2. @echo.>x:\wimpart.txt
  3. @echo select disk "0">>x:\wimpart.txt
  4. @echo clean>>x:\wimpart.txt
  5. @echo convert gpt>>x:\wimpart.txt
  6. @echo create partition efi size=100>>x:\wimpart.txt
  7. @echo format quick fs=fat32 label="System">>x:\wimpart.txt
  8. @echo assign letter="S">>x:\wimpart.txt
  9. @echo create partition msr size=128>>x:\wimpart.txt
  10. @echo create partition primary>>x:\wimpart.txt
  11. @echo format quick fs=ntfs label="Windows">>x:\wimpart.txt
  12. @echo assign letter="W">>x:\wimpart.txt
  13. @echo exit>>x:\wimpart.txt
复制代码
@echo select disk "0">>x:\wimpart.txt

玩PE 系统 ? 玩PE 好像无忧论坛的高手多点吧!
年轻的时候也喜欢自己整PE,现在没啥动力了,,
 楼主| 发表于 2025-5-9 08:53:20 | 显示全部楼层
回复 2# jiavip


    谢谢大佬,我是新手,还停留在应用的阶段,主要是备份和还原win 系统,有其他图形界面的备份与还原的winpe吗?我试了一下傲梅,不稳定不好用
 楼主| 发表于 2025-5-9 09:07:39 | 显示全部楼层
回复 2# jiavip


    for /f "skip=8 tokens=3" %%A in (%WinPESource%Output.txt) do (
if exist %%A:\images\install.txt set InstallPath=%%A:\images&& echo "Found device with images folder and install.txt file"
)
这一句有点看不懂,请
大佬帮忙详解
发表于 2025-5-9 10:35:49 | 显示全部楼层
本帖最后由 flashercs 于 2025-5-9 10:40 编辑

回复 4# dragon123
选择磁盘序号:1
  1. @echo select disk "1">>x:\wimpart.txt
复制代码
关于代码解释,可以让AI给解释代码;
AI的解释:
  1. for /f "skip=8 tokens=3":
  2. for /f: 用于逐行读取文件内容,并对每一行执行命令。
  3. "skip=8 tokens=3": 这些是for循环的选项。
  4. skip=8: 跳过文件开头的前8行,从第9行开始处理。
  5. tokens=3: 每一行按默认分隔符(空格、制表符等)分割后,只取第3个标记(token)赋值给循环变量%%A。
  6. %%A:
  7. 在批处理脚本中,%%A 是循环变量,它会依次被赋予由tokens=3指定的每行的第3个标记的值。注意,在命令行直接运行时只需单百分号(%A),但在批处理脚本(.bat或.cmd文件)中需要双百分号(%%A)。
  8. in (%WinPESource%Output.txt):
  9. 指定要读取的文件路径。这里使用了变量 %WinPESource% 来动态构建文件路径,最终指向名为 Output.txt 的文件。
  10. do (...):
  11. 对于每一行(经过skip和tokens过滤后的),执行括号内的命令。
  12. if exist %%A:\images\install.txt:
  13. 检查是否存在以当前循环变量%%A(代表盘符或其他标识符)为根目录下的\images\install.txt文件。
  14. set InstallPath=%%A:\images:
  15. 如果上述条件满足,则设置环境变量InstallPath为其对应的路径(即%%A:\images)。
  16. && echo "Found device with images folder and install.txt file":
  17. && 表示如果前面的命令成功执行(返回状态码为0),则执行后面的命令。
  18. echo 输出一条消息到控制台,指示找到了包含所需文件夹和文件的设备。
复制代码

评分

参与人数 1技术 +1 收起 理由
Batcher + 1 乐于助人

查看全部评分

 楼主| 发表于 2025-5-9 14:19:00 | 显示全部楼层
回复 5# flashercs


    感谢大佬提醒
发表于 2025-5-10 21:35:44 | 显示全部楼层
本帖最后由 jiavip 于 2025-5-11 01:03 编辑

回复 3# dragon123
称不上大佬吧,

其实像这样的小问题,,随便一个,,,diskpart  /?  就能试探出很多解释了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-3-17 06:01 , Processed in 0.019396 second(s), 8 queries , File On.

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表