1

I have a problem with this code:

for %%n in (%listPath%) do (        
    echo Starting Build
    echo.
    devenv %%n /build Debug
        
    if ERRORLEVEL 1 (
        echo [101;93m ERROR: Error Build Project: %%n [0m
        pause > nul
        exit /b
    )
)

Why does the line echo [101;93m ERROR: Error Build Project: %%n [0m work only outside the if statement? I want to display a red error inside the if.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • That line will not color the text independent of the position, the escape character in front of the sequence is missing – jeb Nov 02 '21 at 16:01
  • the esc tag is in the code, it's stackoverflow that deleted it for me. –  Nov 02 '21 at 16:05
  • the code works if I put it out of the if. –  Nov 02 '21 at 16:06
  • 1
    It's better to use a variable containing an escape character, like `for /F "delims=#" %%a in ('prompt #$E# ^& for %%a in ^(1^) do rem') do set "ESC=%%a" ` and use it `echo %ESC%[101;93m ERROR: %ESC[m` – jeb Nov 02 '21 at 16:10
  • 1
    I suppose your problem is elsewhere. Try a minimal example. First echo it outside the `IF` then inside `IF` but no other code. Probably one of your called programs like `devenv` *destroys* the escape handling – jeb Nov 02 '21 at 16:12
  • @jeb I tried what you suggested but it doesn't work –  Nov 02 '21 at 16:16
  • @jeb yeah, devenv destroys the escape handling. How can i fix it? thanks –  Nov 02 '21 at 16:18
  • You could check if it works with `devenv %%n /build Debug > NUL`. I read something about this problem before, but can't remember where, but probably it was a post/discovery from [@dbenham](https://stackoverflow.com/users/1012053/dbenham) – jeb Nov 02 '21 at 16:22
  • yeah, it works. Thanks a lot –  Nov 02 '21 at 16:24

1 Answers1

0

A minimal example works as expected

@echo off

for /F "delims=#" %%a in ('prompt #$E# ^& for %%a in ^(1^) do rem') do set "ESC=%%a"

echo %ESC%[101;93m Test #1 %ESC%[0m
if 1==1 (
    echo %ESC%[42m Test #2 %ESC%[0m
)

It also works with direct placement of escape characters.


From the comments:
The problem was caused by devenv and can be solved by redirecting the output.

devenv %%n /build Debug > NUL

Description of the underlying problem:
SO: ANSI color Set Graphics Rendition breaking mid-batch and working after it proceeds

jeb
  • 78,592
  • 17
  • 171
  • 225