-1

Hey does anyone know if there is a command or something that allows me to have all the letters, numbers and characters in one?

:start
set input=
set /p input=[Y / N]? 
if %input%==Y goto y
if %input%==N goto n
if %input%==* goto this

:y
goto end

:n
exit

:this
@echo.   Only J or N

:end

Here you can already see that I tried to get everything in one with "*" which unfortunately didn't work...

Thank you for trying to help me but I would like to point out again that I need help to find something that allows me to use all the characters, letters and symbols in one. example:

if I take y then he goes on. if I take n then it closes. and when i use g or h he says: "can't do, just y and n (for yes and no)"

but things like "*" or "%word%" don't work and yes, I have "choice /?" Already tried but when I do it like this it doesn't work either:

for %%i in (
D o " you " k n o w " to " h e l p " me
) do (<nul set /p "=.%bs%%%~i" & >nul ping -n 1 localhost)
@ echo ?

CHOICE

[I don't care about syntax errors or something. if it works then it fits, it's just this one thing to use all characters, letters and symbols in one.] No hate <3

HenryLED
  • 3
  • 2
  • 1
    The `choice` command allows you to restrict input to specified characters. Open cmd..exe and type `Choice /?` to read the usage info – T3RR0R Jan 29 '22 at 13:41
  • 1
    Please read my answer on [How to stop Windows command interpreter from quitting batch file execution on an incorrect user input?](https://stackoverflow.com/a/49834019/3074564) It explains in full details the usage of `set /P` in a safe and secure manner and the usage of `choice` for a choice menu. – Mofi Jan 29 '22 at 14:12
  • 2
    When you already checked for `Y` and `N`, there is no need for another `if` If it didn't jump before, it is anything but `Y` or `N`. I support the suggestion to use `choice` instead. – Stephan Jan 29 '22 at 14:43
  • 1
    Well, Stephan is right, except the user hits just RETURN or ENTER without entering anything at all as in this case the environment variable `input` is still undefined after the user prompt and so the next line becomes `if ==Y goto j` which is an invalid __IF__ condition. For that reason the Windows command processor `cmd.exe` exits processing of the batch file with the error message: "goto was unexpected at this time." The command `goto` is not a valid comparison operator for __IF__ with `==F` being the first argument for command __IF__. Another syntax issue occurs on user enters just `"`. – Mofi Jan 29 '22 at 14:52
  • 1
    The error message on execution of the command line `if "==Y goto j` is in this case: "The syntax of the command is incorrect.". There should be really used `%SystemRoot%\System32\choice.exe`. I recommend further using as label a different string than `start` which is also a [Windows command](https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/windows-commands) as it can be seen on running in a command prompt window `help` and next `help start` or `start /?`. It is possible to use the names of Windows commands as labels, variable names, file names, but don't do that. – Mofi Jan 29 '22 at 14:56

2 Answers2

1

if has no wildcard, but as I already commented, you don't need the third if at all. If Y and N are already handled, it can only be "anything else"

:start
set "input="
set /p "input=[Y / N]? "
if "%input%"=="Y" goto y
if "%input%"=="N" goto n
echo it's Y or N, nothing else&goto :start

The quotes makes it a little bit safer, but note this still isn't safe at all.

Cmd handles several characters (so called "poison chars" like <>|&"!) different (and even more under certain conditions).
A more secure method:

@echo off
setlocal enabledelayedexpansion
:loop
set "input="
set /p "input="
>nul 2>&1 (
echo/!input!|findstr /x "Y" >nul && goto YES
echo/!input!|findstr /x "N" >nul && goto NO
)
echo FAIL
goto :loop
:YES
echo yeah
goto :eof
:NO
echo nope
goto :eof

(Disclaimer: I'm quite sure, someone will find an input-string to make it fail too)

Note: both if and findstr support the /i switch to make them case-insensitive.

Stephan
  • 53,940
  • 10
  • 58
  • 91
0

And just for the sake of showing everyone else how to perform this task using the appropriate commands:

%SystemRoot%\System32\choice.exe
If ErrorLevel 2 Exit /B
Rem The code you had under :end replaces this line
Compo
  • 36,585
  • 5
  • 27
  • 39