[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[其他] [分享]批处理把几段重复的代码改成for循环

【问题描述】


这能搞循环么?
  1. %Dism-10-X64% /Image:%Mount% /Set-Edition:CoreSingleLanguage
  2. %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  3. %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%Desc1% Home SL" "%Desc-EN-US%" --image-property DISPLAYNAME="%Desc1% Home SL" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="CoreSingleLanguage"
  4. %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
  5. %Dism-10-X64% /Image:%Mount% /Set-Edition:CloudEdition
  6. %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  7. %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%Desc1% CloudEdition" "%Desc-EN-US%" --image-property DISPLAYNAME="%Desc1% CloudEdition" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="CloudEdition"
  8. %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
  9. %Dism-10-X64% /Image:%Mount% /Set-Edition:Professional
  10. %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  11. %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%Desc1% Pro" "%Desc-EN-US%" --image-property DISPLAYNAME="%Desc1% Pro" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="Professional"
  12. %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
  13. %Dism-10-X64% /Image:%Mount% /Set-Edition:ProfessionalEducation
  14. %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  15. %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%Desc1% Pro Edu" "%Desc-EN-US%" --image-property DISPLAYNAME="%Desc1% Pro Edu" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="ProfessionalEducation"
  16. %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
  17. %Dism-10-X64% /Image:%Mount% /Set-Edition:ProfessionalSingleLanguage
  18. %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  19. %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%Desc1% Pro SL" "%Desc-EN-US%" --image-property DISPLAYNAME="%Desc1% Pro SL" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="ProfessionalSingleLanguage"
  20. %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
复制代码
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

【解决方案】
  1. for /f "delims=:" %%i in ('findstr /n "^:DynamicScript$" "%~f0"') do (
  2.     for /f "tokens=1-2 delims=:" %%j in ('more +%%i "%~f0"') do (
  3.         %Dism-10-X64% /Image:%Mount% /Set-Edition:%%j
  4.         %Dism-10-X64% /Commit-Image /MountDir:%Mount%
  5.         %Wimlib-imagex-x64% info %Wimfile1%-bak.wim 1 "%%k" "%Desc-EN-US%" --image-property DISPLAYNAME="%%k" --image-property DISPLAYDESCRIPTION="%Desc-EN-US%" --image-property FLAGS="CoreSingleLanguage"
  6.         %Dism-10-X64% /Export-Image /SourceImageFile:%Wimfile1%-bak.wim /SourceIndex:1 /DestinationImageFile:%Wimfile1%.wim
  7.     )
  8. )
  9. goto :eof
  10. REM 这段放在脚本的最下面
  11. :DynamicScript
  12. CoreSingleLanguage:%Desc1% Home SL
  13. CloudEdition:%Desc1% CloudEdition
  14. Professional:%Desc1% Pro
  15. ProfessionalEducation:%Desc1% Pro Edu
  16. ProfessionalSingleLanguage:%Desc1% Pro SL
复制代码
思路概括:
不变的部分,放在for循环里面。
变化的部分,提取出来放在一个配置文件里面,或者放在BAT自身的结尾。

类似用法:
https://mp.weixin.qq.com/s/McvY4CiR-SYGaCha7lmjIw
我帮忙写的代码不需要付钱。如果一定要给,请在微信群或QQ群发给大家吧。
【微信公众号、微信群、QQ群】http://bbs.bathome.net/thread-3473-1-1.html
【支持批处理之家,加入VIP会员!】http://bbs.bathome.net/thread-67716-1-1.html

TOP

返回列表