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

[文本处理] bat 其中一段代码怎么运行2次?

本帖最后由 hentai87 于 2020-6-4 15:22 编辑

不想直接复制一次,以后要是改的话要改2个地方,而且复制2次,会加长代码
顺便问下,怎么优化括号,很多for 和if 弄的括号很多很乱
  1. @echo off
  2. cd /d %~dp0
  3. %1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
  4. taskkill /f /im DismHost.exe
  5. taskkill /f /im dism.exe
  6. setlocal EnableDelayedExpansion
  7. ::-------------------第一次
  8. for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
  9. for /f "skip=%n% delims=" %%b in (..\set.txt) do (
  10. for /f "tokens=1,2* delims=: " %%A in ('%%b\Bin\x64\DISM10\dism.exe /NoRestart /image:%%b\Mount\Install /Get-Capabilities') do (
  11. if "%%A %%B"=="Capability Identity" (
  12. set CapId=%%C
  13. ) else if "%%A"=="State" if "%%B"=="Installed" (
  14. cd. >list.txt
  15. echo !CapId!>>list.txt
  16. findstr /g:black_list.txt /v list.txt 2>nul
  17. if errorlevel 2 (
  18. for /f "delims=" %%i in (list.txt) do (
  19. %%b\Bin\x64\DISM10\dism.exe /image:%%b\Mount\Install /Remove-Capability /CapabilityName:"%%i"
  20.   )
  21. ) else (
  22. for /f "delims=" %%i in ('findstr /g:black_list.txt /v list.txt 2^>nul') do (
  23. %%b\Bin\x64\DISM10\dism.exe /image:%%b\Mount\Install /Remove-Capability /CapabilityName:"%%i"
  24. )
  25. ))))
  26. ::-------------------第二次
  27. endlocal
  28. pause
  29. exit /b
复制代码

set a=0
:run
...
set /a a+=1
if %a% lss 2 goto :run

TOP

你这括号太乱了,我尽力了
注意缩进和对齐,多换几行也无所谓,主要是要看的清楚
  1. @echo off
  2. cd /d %~dp0
  3. %1 start "" mshta vbscript:createobject("shell.application").shellexecute("""%~0""","::",,"runas",1)(window.close)&exit
  4. taskkill /f /im DismHost.exe
  5. taskkill /f /im dism.exe
  6. setlocal EnableDelayedExpansion
  7. set a=0
  8. :run
  9. for /f "delims=:" %%a in ('findstr /n msmg设置 ..\set.txt') do set n=%%a
  10. for /f "skip=%n% delims=" %%b in (..\set.txt) do (
  11. for /f "tokens=1,2* delims=: " %%A in ('%%b\Bin\x64\DISM10\dism.exe /NoRestart /image:%%b\Mount\Install /Get-Capabilities') do (
  12. if "%%A %%B"=="Capability Identity" (
  13. set CapId=%%C
  14. ) else if "%%A"=="State" if "%%B"=="Installed" (
  15. cd. >list.txt
  16. echo !CapId!>>list.txt
  17. findstr /g:black_list.txt /v list.txt 2>nul
  18. if errorlevel 2 (
  19. for /f "delims=" %%i in (list.txt) do (
  20. %%b\Bin\x64\DISM10\dism.exe /image:%%b\Mount\Install /Remove-Capability /CapabilityName:"%%i"
  21. )
  22. ) else (
  23. for /f "delims=" %%i in ('findstr /g:black_list.txt /v list.txt 2^>nul') do (
  24. %%b\Bin\x64\DISM10\dism.exe /image:%%b\Mount\Install /Remove-Capability /CapabilityName:"%%i"
  25. )
  26. )
  27. )
  28. )
  29. )
  30. set /a a+=1
  31. if %a% lss 2 goto :run
  32. endlocal
  33. pause
  34. exit /b
复制代码

TOP

返回列表