Coding issues

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jackjack
Posts: 3
Joined: 26 Apr 2018 14:19

Coding issues

#1 Post by jackjack » 26 Apr 2018 14:28

If anyone can help me this would be great.

so I am needing to do a simple check after a user has inputted information,

I only want the check to be approved if they have only entered 6 colours if not it fails.

I was trying to do it in 6 different IF statements but its to clunky

Code: Select all

set /p "myVar=---> " 
echo %myVar%|findstr /ix "red:Red:blue:Blue">nul && ( 
 echo %myVar% matched 
 ) || ( 
 echo %myVar% not matched 
 )
above is what I tried to get working however I don't think it is picking up all the colors and I get an error message saying 'the system can not fine the system file blue'

if anyone has got any ideas on how this can be fixed that would be great.

many thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Coding issues

#2 Post by Squashman » 26 Apr 2018 14:32

You have not really specified how the user is entering those 6 colors. You are also only checking two colors.

You also have not provided any feedback on all the help that has been provided to you on StackOverFlow.
https://stackoverflow.com/questions/500 ... user-input

jackjack
Posts: 3
Joined: 26 Apr 2018 14:19

Re: Coding issues

#3 Post by jackjack » 26 Apr 2018 14:38

So it will ask the user to enter a color, and it can be 6 colors but I need to make sure that it is only them 6 colors,

so they will be set in a variable already defined

jackjack
Posts: 3
Joined: 26 Apr 2018 14:19

If statment

#4 Post by jackjack » 26 Apr 2018 15:01

If any one is able to help me

I am using this code

Code: Select all

if NOT "%color%"=="blue" (
 GOTO :colorError
 ) else (
 echo working
 )
but I want to add green and red into the statement, how can I do this please

many thanks

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Coding issues

#5 Post by Squashman » 26 Apr 2018 18:46

Give this a try.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "colors=redorangeyellowgreenblueindigo"
set /p "ucolor=Enter a color--->"

IF "!colors:%ucolor%=!"=="%colors%" (
	echo %ucolor% is an invalid color
) else (
	echo Your color is: %ucolor%
)
pause

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Coding issues

#6 Post by Aacini » 26 Apr 2018 20:50

Squashman wrote:
26 Apr 2018 18:46
Give this a try.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "colors=redorangeyellowgreenblueindigo"
set /p "ucolor=Enter a color--->"

IF "!colors:%ucolor%=!"=="%colors%" (
	echo %ucolor% is an invalid color
) else (
	echo Your color is: %ucolor%
)
pause
Your method is good. However, each value should be separated by a delimiter character; otherwise it would be too many wrong values that would be accepted as valid, like "do", "edo", "edor", and a long et cetera...

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "colors=/red/orange/yellow/green/blue/indigo/"
set /p "ucolor=Enter a color--->"

IF "!colors:/%ucolor%/=!" == "%colors%" (
	echo %ucolor% is an invalid color
) else (
	echo Your color is: %ucolor%
)
pause
Antonio

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Coding issues

#7 Post by Squashman » 26 Apr 2018 21:02

I agree Antonio. So lets take it a step further. :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "colors=/red/orange/yellow/green/blue/indigo/"
set /p "ucolor=Enter a color--->"
set "empty="
set "delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
FOR /F "delims=%delims%" %%G IN ("%ucolor%") DO SET "empty=%%G"

IF DEFINED emtpy (
	echo invalid entry
	pause
	GOTO :EOF
)

IF "!colors:/%ucolor%/=!" == "%colors%" (
	echo %ucolor% is an invalid color
) else (
	echo Your color is: %ucolor%
)
pause
This basically only protects against them entering: red/orange as input.

Aacini
Expert
Posts: 1914
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Coding issues

#8 Post by Aacini » 27 Apr 2018 00:48

Squashman wrote:
26 Apr 2018 21:02
I agree Antonio. So lets take it a step further. :D

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "colors=/red/orange/yellow/green/blue/indigo/"
set /p "ucolor=Enter a color--->"
set "empty="
set "delims=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
FOR /F "delims=%delims%" %%G IN ("%ucolor%") DO SET "empty=%%G"

IF DEFINED emtpy (
	echo invalid entry
	pause
	GOTO :EOF
)

IF "!colors:/%ucolor%/=!" == "%colors%" (
	echo %ucolor% is an invalid color
) else (
	echo Your color is: %ucolor%
)
pause
This basically only protects against them entering: red/orange as input.
I think it is much simpler to just change the IF command in this way instead:

Code: Select all

IF "!colors:/%ucolor:/=%/=!" == "%colors%" (
Antonio

Post Reply