0
 set /p codename="Please enter the codename! "

 if exist %codename% = Candy, Sugar, Lollipop (
    echo The code you entered is not available.
 )

If the user enters %codename% as Candy, Sugar or Lollipop etc. Is there a way to make the echo say "The code you entered is not available" and then pause the bat?

If the user enters anything else except these three, the script will continue working.

How can I do this?

  • 1
    Why are you working with batch & not PowerShell? If you don't know batch, there's no point in learning it now unless required for a job. If you're out to improve your skill & understanding, focus on PowerShell. – Keith Miller Feb 2 at 5:35
  • 1
    Why people keeps asking me why I'm using bat? It's because it's easier for me. I have dyslexia and I can't learn any coding languages as C#, Python or anything else. Bat is what I've used for most of the basic stuff and I don't think I'm gonna stop using it. – Nathan Boutroux Feb 2 at 16:43
1

You can use multiple string to validate user input with findstr:

echo/%codename% |%__APPDIR__%findstr.exe "Candy Sugar Lollipop" >nul && goto :Next

1) echo/%codename% (string-input) to findstr (find string),

2) Use /i case-insensitive if you need, if not, remove /i

3) If matched, goto :next label... if not, will execute next line…

4) Echo/your message and a timeout show up to user, waiting for user to press any key

5) goto :EOF (end of file), same as exit/abort/quit your bat.


@echo off & setlocal

set /p codename="Please enter the codename! "

echo/%codename% |%__APPDIR__%findstr.exe "Candy  Sugar  Lollipop" >nul && goto :Next
echo/The code you entered is not available^!! & %__APPDIR__%timeout -1 & goto :EOF

:Next
rem ::  your code continue here...`

  • Obs. If user input only enter, you can limit this in: if !_cnt! equ 3
@echo off & setlocal EnableDelayedExpansion 

:loop

set /p codename="Please enter the codename! "

set /a "_cnt+=1+0"

if !_cnt! equ 3 (
    echo/Maximum number of attempts allowed exceeded^!!
    goto :Error ) else if "!codename!"=="" goto :loop

echo/!codename! |%__APPDIR__%findstr.exe "Candy  Sugar  Lollipop" >nul && goto :Next

:Error
echo/The code you entered is not available^!! & %__APPDIR__%timeout -1
endlocal & goto :EOF

:Next
rem ::  your code continue here...
|improve this answer|||||
1

If exist is the wrong form of comparison for variable content. The correct form would be:

If "%variable%"=="desired value" (command)

If the same command is to be executed on any given allowed value, you can use a for loop like this:

    For %%A In (Candy,Sugar,Lollipop) Do (
        If  /I "%codename%"=="%%A" (
            Call :CodeTrue
        )
    )

As a function at the end of your script

:CodeTrue
Pause
Rem other commands
Exit /b
|improve this answer|||||
  • To protect against troublesome characters, use of Delayed expansion is advised, and replace %variable% with !variable! – T3RR0R Feb 2 at 5:22
1

If you can move to PowerShell:

$NewName = Read-host -Prompt 'Please enter the codename!'
If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) { echo 'The code you entered is not available.'}


PS C:\> $NewName = Read-host -Prompt 'Please enter the codename!'
>> If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) { echo 'The code you entered is not available.'}
    Please enter the codename!: Candy
The code you entered is not available.
PS C:\>  

Do you need a true environmental variable?

$NewName = Read-host -Prompt 'Please enter the codename!'
If ($NewName -in ('Candy', 'Sugar', 'Lollipop') ) {
   echo 'The code you entered is not available.'
}
Else {$env:CodeName = $NewName} # only exists within scope of procesd
    or
Else {[Environment]::SetEnvironmentVariable("CodeName", $NewName, "User")} # Persistant at user-level
    or (Requires Admin PowerShell Console. Not avaiable to creating process.)
Else {[Environment]::SetEnvironmentVariable("CodeName", $NewName, "Machine")} # Persistant at machine-level
|improve this answer|||||

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.