Search code examples
windowsbatch-filestring-comparisondouble-quotes

compare a character with double quotes " in windows batch


I want to compare a single character with double quotes " in a in windows batch file.

@echo off
setlocal EnableDelayedExpansion
set var=before
for /f %%a in (zen.csv) do (
    set var=%%a
    echo !var:~-1!
    IF "!var:~-1!"=="""" (
        @echo found
    )
)
pause

the result is like:

0
1
"
"
"
0
1
2

As you can see, the batch did not output the "found". How to compare a character with double quotes?


Solution

  • @echo off
    setlocal EnableDelayedExpansion
    set var=before
    for /f %%a in (zen.csv) do (
        set var=%%a
        echo !var:~-1!
        IF !var:~-1!==^" (
            @echo found
        )
    )
    pause
    

    Escape a double quote with a caret ^ to become ^". Omit the outer double quotes for the comparison. The caret will escape many characters except for % which needs to be escaped by doubling up to become %%.