Search code examples
batch-fileif-statementcmdspecial-characters

How to detect if input is quote?


I have the following code

if "%userInput%"==""" (
    do_something
)

I would like it to detect if the %userInput% is a quote("). However, this code throws an error.

How to detect if input is a quote?


Solution

  • Here is a solution without delayed expansion that should work with any input, including spaces and poison characters. My test code is in a loop. When you are ready to quit, simply press <Enter> without typing anything.

    @echo off
    setlocal
    
    :loop
    set "var="
    set /p "var=enter a string: "
    if not defined var exit /b
    
    if "%var:"=""%" == """" echo equals quote
    
    echo(
    goto loop
    

    The trick should be fairly obvious. Enclose the value in quotes, and double all internal quotes during expansion, such that all token delimiters and poison characters are guaranteed to be quoted. Of course on the right side you must include the enclosing quotes, plus the doubled quote you are trying to match (for a total of 4).

    If you need to be able to pass all input (including no value) through the IF test, then you can define a test variable. Something like the following (assume var already has the user input)

    set "test="
    if defined var set "test=%var:"=""%"
    if "%test%" == """" echo equals quote
    

    Alternate solution using FINDSTR

    I was inspired by Stephan's answer to find an alternative solution using FINDSTR that does not require delayed expansion. He interpreted the question differently, answering the question "Does the input value contain a quote anywhere within it?", and solved that using FIND.

    But FIND cannot determine if the input exactly matches a single quote character.

    A simple one liner with FINDSTR can solve this.

    2>nul set var|findstr /x ^"var=\"" >nul && echo equals quote
    

    or

    2>nul set var|findstr /x "var=\"^" >nul && echo equals quote
    

    Note that FINDSTR generally requires quotes to be escaped as \", and the batch parser requires one of the quotes to be escaped as ^" because there are an odd number of quotes. If a quote is not escaped, then the remainder of the line is considered to be part of the string instead of redirection and conditional execution operations.

    Also note that with the batch parser, it is impossible to escape a quote once quoting is activated. So the following does NOT work

    2>nul set var|findstr /x "var=\^"" >nul && echo equals quote THIS DOES NOT WORK