0

I need to convert user input from cmd into lowercase before my script can use it , so i can use it in a If statement but i don't know exactly how , i tryed to just compare the input with the most common inputs that the user may write but i want to cover all the posibilities.

here is the code i wrote so far :

set colour=Default
echo Please choose one of the supported colours for the name(Red,Blue or 
Green)
       :WrongColour
   set /p colour=
                if %colour%== Red   (
                                 goto :SuportedColour
)          else if %colour%== red   (
                                     goto :SuportedColour
)          else if %colour%== RED   ( 
                                     goto :SuportedColour
)          else if %colour%== Blue  (
                                      goto :SuportedColour
)          else if %colour%== blue  (
                                      goto :SuportedColour
)          else if %colour%== BLUE  (
                                      goto :SuportedColour
)          else if %colour%== Green (
                                      goto :SuportedColour
)          else if %colour%== green (
                                      goto :SuportedColour
)          else if %colour%== GREEN (
                                      goto :SuportedColour
)

Is there a much easier way to just convert everything into lowercase so then i can compare to it and proceed to the next stage in my script if yes?

Gerhard
  • 22,678
  • 7
  • 27
  • 43
Andrei
  • 3
  • 1
  • 2
  • 1
    The easiest way in batch is this, given that character codes above `0x7F` are ignored (e. g., `à`): `for %%L in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do call set "colour=%%colour:%%L=%%L%%"`. However, using `if` **`/I`** `%colour%==green`, or even better, `if` **`/I`** `"%colour%"=="green"` does a case-**i**nsensitive comparison. Nevertheless, take a look at the [`choice` command](http://ss64.com/nt/choice.html). – aschipfl May 07 '18 at 17:06
  • Possible duplicate of [How to convert the value of %USERNAME% to lowercase within a Windows batch script?](https://stackoverflow.com/questions/284776/how-to-convert-the-value-of-username-to-lowercase-within-a-windows-batch-scrip) –  May 07 '18 at 17:21
  • you might be interested in the [choice](https://ss64.com/nt/choice.html) command. – Stephan May 07 '18 at 18:10

1 Answers1

2

The if /I swhich is what you want:

@echo off
set colour=Default
set /p "colour=Please choose one of the supported colours for the name(Red,Blue or Green)"
if  /i "%colour%" == "red" goto :SupportedColour
if  /i "%colour%" == "blue" goto :SupportedColour
if  /i "%colour%" == "green" goto :SupportedColour
echo %colour% is not supported..
goto :EOF
:SuportedColour
echo You chose a supported colour: %colour%

I however see you have only 1 label you goto which is SupportedColour so I suspect you only want use a single label if any of those colours are what entered, therefore a for loop might be a better option:

@echo off
set colour=Default
set "mycolours=blue red green"
set /p "colour=Please choose one of the supported colours for the name(Red,Blue or Green)"
for %%i in (%mycolours%) do if /i "%%i" == "%colour%" goto :SupportedColour
echo %colour% is not supported
goto :EOF
:SupportedColour
echo You chose a supported colour: %colour%

Here you however do not need to have a goto either, but I added it as I am unsure of what the rest of your code does.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • thanks , i've done it using /I , it is easier and I used it also in other parts of my script where i needed an case-insensitive comparation. – Andrei May 07 '18 at 18:21