0

I want to initially check for DISM health, then based on result, I want to select 1 (to run DISM restore & sfc) or 2 (run only sfc).

I cannot get this to work. Any guidance would be appreciated.

Edit: Maybe there's a way to do this all automatically? Execute choice based on initial result?

@ECHO OFF
:BEGIN
CLS

Dism /Online /Cleanup-Image /CheckHealth
timeout /t -1

ECHO.

CHOICE /N /C:12 /M "Press '1' to Run DISM/RestoreHealth & SFC, or Press '2' to run only SFC"%1

ECHO.

IF ERRORLEVEL ==1 GOTO 1
IF ERRORLEVEL ==2 GOTO 2
GOTO END

:2
sfc/scannow
GOTO END

:1
Dism /Online /Cleanup-Image /RestoreHealth
sfc/scannow

:END
pause
Scott
  • 61
  • 1
  • 8
  • Your `if `syntax is wrong. It's either `if errorlevel 2 goto 2` first, then `if errorlevel 1 goto 1` (see `if /?` for the why) or `if %errorlevel% == 1 goto 1` – Stephan Aug 16 '23 at 13:58
  • on a second thought - why don't you use `choice`'s errorlevel directly? `choice ...` and `goto %errrorlevel%` – Stephan Aug 16 '23 at 14:31
  • Thank you @Stephan... how would that look? (your second comment) – Scott Aug 16 '23 at 14:35
  • just `goto %errorlevel%` the line after the `choice` command. No ìf`'s at all. – Stephan Aug 16 '23 at 14:39
  • Stephan, I'm still confused... how would the script know what to do? Would you please write what one of the lines would then look like, thanks. – Scott Aug 16 '23 at 16:07
  • @Scott - `goto %errorlevel%` _is_ the line. You reference variables in batch like `%variable%`. – SomethingDark Aug 16 '23 at 16:48

1 Answers1

1

(comments is a bad place to show code, so...)

choice returns an errorlevel. You do several if %errorlevel% == N goto N - not needed as your labels and errorlevels are the same. You can just goto %errorlevel% after the choice command:

...
CHOICE /N /C:12 /M "Press '1' to Run DISM/RestoreHealth + SFC, or Press '2' to run only SFC"
goto :%errorlevel%
GOTO :END
:0
echo you cancelled the question.
goto :end
:1
echo running dism and sfc
goto :end
:2
echo running sfc only
goto :end
:end
pause

(for explanation: choice returns an errorlevel of 0 when you press CTRL-C (and answer the following question "Termiate batch job" with "no" - if you answer "yes", the script immediately ends))

The colon in goto is optional. I personally use it to be consistent with the call command and make it clearly visible as a label. There are other opinions about that - pick your choice, but try to stay consistent for readabilty.

Stephan
  • 53,940
  • 10
  • 58
  • 91