Andrei
Andrei

Reputation: 3

Convert user input into lowercase in batch file

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?

Upvotes: 0

Views: 3816

Answers (1)

user7818749
user7818749

Reputation:

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.

Upvotes: 2

Related Questions