You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

批处理脚本优化需求:完善SFC、DISM及自定义源路径输入功能

修复后的Windows批处理脚本

以下是满足需求的完整批处理代码,解决了选项3、4的功能问题,并修正了原脚本的逻辑缺陷:

@echo off
setlocal enabledelayedexpansion

echo 1. SFC Scan Utility
echo 2. DISM [Using Windows Update]
echo 3. DISM [Using Mounted Image]
echo 4. DISM [Using Own Source]

:: 修正choice命令,添加选项4支持
choice /c:1234 /m "请选择操作选项:"

If errorlevel 4 if not errorlevel 5 goto LAccess2
If errorlevel 3 if not errorlevel 4 goto LAccess
If errorlevel 2 if not errorlevel 3 goto Dism
If errorlevel 1 if not errorlevel 2 goto Sfc

:Sfc
echo. & echo  Running [SFC] . . .
sfc.exe /scannow
goto EndScript

:Dism
echo. & echo  Running [DISM] . . .
Dism /Online /Cleanup-Image /RestoreHealth
goto EndScript

:LAccess
echo. & echo  Running [DISM Mounted Image] . . .
:: 提示用户输入离线镜像路径和源路径
set /p "ImagePath=请输入离线镜像路径(示例:C:\offline):"
set /p "SourcePath=请输入源路径(示例:c:\test\mount\windows):"
:: 执行针对离线镜像的DISM命令
Dism /Image:"!ImagePath!" /Cleanup-Image /RestoreHealth /Source:"!SourcePath!"
goto EndScript

:LAccess2
echo. & echo  Running [DISM Own Source] . . .
:: 提示用户输入源路径
set /p "SourcePath=请输入源路径(示例:c:\test\mount\windows):"
:: 执行带LimitAccess的在线DISM命令
Dism /Online /Cleanup-Image /RestoreHealth /Source:"!SourcePath!" /LimitAccess
goto EndScript

:EndScript
echo. & echo ^>Press ANY key to EXIT . . . & pause >nul & Exit

关键修改说明

  • 修正choice命令:将choice /c:123改为choice /c:1234,支持选择选项4;调整errorlevel判断顺序(必须从高到低判断,因为errorlevel是满足大于等于指定值即触发);
  • 添加用户输入功能:在选项3、4的标签中使用set /p命令提示用户输入路径,通过延迟变量扩展(enabledelayedexpansion)确保变量正确读取;
  • 分支终止逻辑:每个标签执行完命令后通过goto EndScript跳转到统一的结束部分,避免执行后续无关代码;
  • 区分命令逻辑:选项3针对离线镜像(使用/Image参数),选项4针对在线系统(使用/Online+/LimitAccess),匹配各自的功能定位;
  • 路径引号处理:给路径变量添加双引号,避免路径包含空格时出现命令执行错误。

内容的提问来源于stack exchange,提问作者Edward Kenway

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 04:25:21