Search code examples
windowsvariablesfor-loopbatch-filequotes

How to handle variable values with quotes in windows batch files


i have some problems with string quotes in a windows batch file. I try to parse a csv file (, seperated) and multiline entries. I have to write out only the strings after the fouth sperator as a text file. The result has to include the new lines and the quotes.

Input:

"1","50","1","Warning! Q1: Value too high [W-0001]"
"2","49","1","Warning! Q1: Value too low [W-0002]"
"3","48","1","Warning! Q1: Value changing too fast.
Check for endless loop[W-0003]"

Output:

 "Warning! Q1: Value too high [W-0001]"
 "Warning! Q1: Value too low [W-0002]" 
 "Warning! Q1: Value changing too fast.
 Check for endless loop[W-0003]"

This work fine for the strings without new lines.

for /F "tokens=1-4 skip=4 delims=," %%a in (input.csv) do @echo %%d >> output.txt

I tried this but i got a syntax error

for /F "delims=, tokens=1-4" %%a in (input.csv) do call :loopbody %%a %%b %%c %%d
goto :eof

if [%4]==[] goto :test
echo %4
goto :eof

:test
echo %1

Error because of the quote at the beginning of the string

Syntaxfehler.
C:\>if ["Warning Value changing too fast.]==[] goto :test

How to handle variable values with quotes?


Solution

  • This works with your data:

    @echo off
    for /F "delims=, tokens=1-3,*" %%a in (input.csv) do (
    set "aa="
    set "aa=%%d"
       if not defined aa (
            >>file.out echo %%a
         ) else (
            >>file.out echo %%d
       )
    )