0

I need this script to run the commands when the specific number is typed, but only the first command works.

Whenever I enter a number that is not one, it immediately just clears the screen and restarts the script.

Here is the code:

@echo off

:optimizer
color F0
echo Welcome to Bug-Blitz!
echo.
echo 1 [Fix Registry Errors (It will take a few seconds)]
echo.
echo 2 [Storage Cleanup]
echo.
echo 3 [Refresh IP (It will take a few seconds)]
echo.
echo 4 [Restart System]

set /p tweaks=Enter a number from above:  

if %tweaks% == 1 DISM /Online /Cleanup-Image /CheckHealth
cls 
goto optimizer 

if %tweaks% == 2 del C:\Windows\prefetch\*.*/s/q
pause
cls 
goto optimizer

if %tweaks% == 3 ipconfig /release && ipconfig /renew 
pause
cls 
goto optimizer

if %tweaks% == 4 shutdown /r 
pause
cls 
echo Your computer will restart in a few moments...
pause
exit

I tried to run the program using admin mode but it still won't work. I even tried a VM but it still wouldn't work.

How do I fix this problem? Anyone has any idea or way?

Compo
  • 36,585
  • 5
  • 27
  • 39
  • Hi, try: `echo Enter number here... set /p tweaks=` – Dxg125 Apr 26 '23 at 06:53
  • Please include your code as text in the question itself, instead of posting pictures. – VLL Apr 26 '23 at 06:54
  • Your title say nothing about what you are asking. Also your tags: not very useful. Please learn to provide information (not too much, but enough and in the right place) so that relevant people could help you. – Giacomo Catenazzi Apr 26 '23 at 07:01
  • Batch executes code line-by-line. Whether `tweaks` is 1 or not, the next line after `if…1` is literally that, the `cls` line directly following the `if`. Please consider `choice` which is designed for this task. Use the search facility for [batch-file] choice eg Gerhard's example https://stackoverflow.com/a/58370914/2128947 or see the documentation - `choice /?` from the prompt. – Magoo Apr 26 '23 at 07:33

2 Answers2

0

First the answer of @Magoo is right but let me add something:

You can just use parenthesis to apply your "if" in multiple lines. And don't forget to put your tests between quotation marks and next to the ==

Example :

if "%tweaks%"=="1" (
     DISM /Online /Cleanup-Image /CheckHealth
     cls 
     goto optimizer 
)

if "%tweaks%"=="2" (
     del C:\Windows\prefetch\*.*/s/q
     pause
     cls 
     goto optimizer
)

Etc.

Feel free to ask for further explanation and have fun !

0

using set /p for choice related commands is not a suitable option as it is not protected from sending other characters. Also, it requires the additional keystroke of the Enter command.

There however exists a command called choice which allows you to only accept the relevant keys you specify, example:

@echo off

:optimizer
color F0 & cls
echo Welcome to Bug-Blitz!
for %%i in ("1. [Fix Registry Errors (It will take a few seconds)]", "2. [Storage Cleanup]", "3 [Refresh IP (It will take a few seconds)]", "4. [Restart System]") do (
   echo %%i
   echo(
)
if %errorlevel% eq 0 echo interupted & exit /b 1
choice /c 1234 /m "select number"
goto tweak_%errorlevel%

:tweak_1
DISM /Online /Cleanup-Image /CheckHealth
goto optimizer

:tweak_2
del "C:\Windows\prefetch\*.*" /s/q
goto optimizer

:tweak_3
echo "ipconfig /release && ipconfig /renew"
goto optimizer

:tweak_4
echo Your computer will restart in a few moments...
shutdown /r

Note, you can remove cls from the script to see the actual output, if required.

Gerhard
  • 22,678
  • 7
  • 27
  • 43