-3

each time someone enters C,c,D or d they still get the invalid entry message even tho its valid

@echo OFF 
Echo Bienvenido
Echo.
echo Este batch es para saber si quieres Disk Clean Up o desea hacer un Disk Defragmenter
echo.
pause
cls
echo. 
set C= DiskCleanUp
set D= DiskDeFragmenter
:menu
echo --------------------------------------
echo Escoge C para cleanup o D para Defrag 
echo --------------------------------------
set /p Option= Ingrese C o D:
echo -------------------------
:choice
If %Option%== C start %windir%\system32\cleanmgr.exe Else goto :incorrect
If %Option%== c start %windir%\system32\cleanmgr.exe Else goto :incorrect
If %Option%== D start %windir%\system32\dfrgui.exe Else goto :incorrect
If %Option%== d start %windir%\system32\dfrgui.exe Else goto :incorrect
:incorrect
    cls
    echo.
    echo Invalid Entry
    pause
    goto :menu

echo. 
cls
Echo Gracias por usar este batch de Eduardo Figueroa.
echo.
echo Press Enter para cerrar.
echo . 
pause
echo.
Exit

each time someone enters C,c,D or d they still get the invalid entry message even tho its valid

  • The spaces in `If %Option%== C` (and the others) are getting included in the comparison, so you need to remove them. Also, you should be using quotes around both things in case the user doesn't enter anything. `If "%Option%"=="C"`. Also also, you can use the `/I` flag so that you can do a case-insensitive comparison. `If /I "%Option%"=="C"`. Also also also, every single `else` part there needs to be removed because once you get the `if` statement corrected, literally anything that isn't `C` will be considered incorrect because the first `if` is returning false so it goes to `:incorrect.` – SomethingDark Nov 27 '22 at 03:02
  • ok I fixed everything you said and it makes it MUUUUCH BETTER!!! but even i type in C and it opens disk clean up i still get invalid entry message from :incorrect no matter what i so the incorrect message always shows up! example i enter C it open the disk cleaner and also gives invalid entry message and if i press any key it goes back to :menu – Eduardo Figueroa Nov 27 '22 at 03:20
  • You have to remember that labels are just signposts and don't actually stop the flow of the script from continuing. If you want the script to go somewhere else after starting `cleanmgr` or `dfrgui`, then you need another command to tell the script to go there. – SomethingDark Nov 27 '22 at 03:34
  • could i get an example i change this and it still doesnt work If /I "%Option%"=="C" start %windir%\system32\cleanmgr.exe goto :Correct If /I "%Option%"=="D" start %windir%\system32\dfrgui.exe goto :Correct :incorrect cls echo. echo Invalid Entry pause goto :menu :Correct – Eduardo Figueroa Nov 27 '22 at 03:40
  • You'd usually use parentheses to run multiple commands, like `if /I "%option%"=="C" (` and then `start %windir%\system32\cleanmgr.exe` and then `goto :Correct` and then `)`, with each of those four things being on a separate line. – SomethingDark Nov 27 '22 at 05:20
  • 1
    @SomethingDark : "The spaces in If %Option%== C (and the others) are getting included in the comparison, " Oh no they're not, dark one! The parsing logic for an `if` comparison statement is very specific; `if [/i][ NOT] arg1 op arg2` where `/i` and ` not` are optional, but **must** if used, be used in that order. `op` may be any of `==`, `neq`, `equ`, `gtr`, `geq`, `lss`, `leq`. The separators are normally spaces, but may be any separator. A separator is required before `not` and on both sides of any operator except "==". – Magoo Nov 27 '22 at 05:38

1 Answers1

2

The problem is that for this statement

If %Option%== C start %windir%\system32\cleanmgr.exe Else goto :incorrect

if Option has been set to C, then what is executed is

start %windir%\system32\cleanmgr.exe Else goto :incorrect

else is simply a string. To do what was intended by that statement, you need

If %Option%== C (start %windir%\system32\cleanmgr.exe) Else (goto :incorrect)

This tells cmd that (start %windir%\system32\cleanmgr.exe) is what to do if the comparison yields true, and (goto :incorrect) is what to do if the comparison yields false.

Note that the colon is not required. (IMHO, for goto, the colon should only be used in a goto :eof statement, where :eof has the defined meaning end-of-file)

The problen with making this correction is that if %option% is not C, then you execute the else part, and proceed to :incorrect. The way to fix that is to remove the else clause entirely, leaving

If %Option%== C (start %windir%\system32\cleanmgr.exe)

where the parentheses are not required since just one statement is being executed.

Even with all the statements modified to the pattern

If %Option%== C start %windir%\system32\cleanmgr.exe

and the /i option applied, the batch would then "flow through" to the label :incorrect and produce the error message since it's not told to goto menu if C/D is chosen.

Therefore you need

If %Option%== C start %windir%\system32\cleanmgr.exe&goto menu

where & separates a series of commands.

Next a couple of tips, for peace in batch-land.

Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign " or a terminal backslash or Space. Build pathnames from the elements - counterintuitively, it is likely to make the process easier. If the syntax set var="value" is used, then the quotes become part of the value assigned.

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
  • 77,302
  • 8
  • 62
  • 84