How to make decision structure to change color in a bat?

Asked

Viewed 4,322 times

5

Hello, I would like to know how to make a decision in the following situation: I have a cmd window with a constant ping, I wanted the script to analyze when the ping is normal the letters of cmd are in the color green, if it was disconnected change to red...

  • 1

    I believe it is not possible, when you "invoke" an application, the script will only have the "power" to do something, after this application closes.

  • in principle would have to make a loop, and individual pings within the loop.

  • and probably use an auxiliary program to change the color individually. COLOR changes the whole screen.

2 answers

11


Important to note that to change the color individually per line, you need some helper program.

The rest is basically that:

//salvar como cping.bat ou coisa do tipo
//uso: cping <IP>

@ECHO off
:top
PING -n 1 %1% | find "%1%"
IF NOT ERRORLEVEL 1 GOTO yes
IF ERRORLEVEL 1 GOTO no
:yes
COLOR 2
ECHO Ping OK
GOTO top
:no
COLOR 4
ECHO Ping FAIL
GOTO top

Note that in the case, the color of the whole window changes, to make individual lines of different colors, it is necessary to change the COLOR by some external utility.

Obviously it’s just an outline, but it already has the complete logic for you to adapt as you see fit.

The line IF NOT ERRORLEVEL 1 GOTO yes was more to show the syntax, actually the GOTO no theoretically should suffice.

The syntax of the command is:

COLOR <cor de fundo + cor da letra em hexadecimal>

Colors available in hexadecimal:

0 preto         8 cinza escuro (seria o "preto claro" kkk)
1 azul          9 azul claro
2 verde         a verde claro
3 ciano         b ciano claro
4 vermelho      c vermelho claro
5 magenta       d magenta claro
6 amarelo       e amarelo claro
7 cinza         f branco

In other words, each Nibble (half byte) has 4 bits in this order: [I~] Intensity, [R] Red, [G] Green, [B] Blue:

IRGB -> binário 1011 -> hexadecimal "e" -> Amarelo claro

Effectively generating a value in this format

I R G B  I R G B
^fundo^ ^frente^

Example to change the letters to strong yellow and dark green background:

color 2e

(2e in hexadecimal is 00101110, or G + IRG background )

Further reading:

How hexadecimal numbers work?


Coloring the output lines individually

Here’s a utility that does ECHO colorful:

http://www.codeproject.com/Articles/17033/Add-Colors-to-Batch-Files

Follows the code adapted to colored lines separately, using the mentioned utility:

@ECHO off

:top
CECHO {08}
PING -n 1 %1% | find "%1%"

IF NOT ERRORLEVEL 1 GOTO yes
IF ERRORLEVEL 1 GOTO no

:yes
CECHO {0A}Ping OK
GOTO top

:no
CECHO {0C}:Ping FAIL
GOTO top

1

inserir a descrição da imagem aqui

Something like that?

    @echo off && setlocal enableextensions enabledelayedexpansion

(

if exist "%temp%\bg.b64" del /q /f "%temp%\bg.b64"
if exist "%temp%\bg.exe" del /q /f "%temp%\bg.exe"

) >nul 2>&1 

echo/ & echo/  && call :_base64_to_exe && Certutil.exe -decode "%temp%\bg.b64" "%temp%\bg.exe" >nul 2>&1

if exist "%temp%\bg.exe" ( 

     set _bg="%temp%\bg.exe"

     for %%I in (8.8.8.8 1.1.1.1 2.2.2.2) do (

         for /f "tokens=4 delims=^=" %%i in ('ping -n 1 %%I ^| findstr /i /c:"ms"') do (

             !_bg! print \n && set "_ms=%%~i"&& set /a _ms=!_ms:ms=!

            )

             if  "!_ms!" geq "0" (

                 !_bg! print f " Host" b " online" a " %%I" c ":" e  " !_ms!" a "ms"

                 echo/ & echo/ 

                 set _ms=

                ) else ( 

                 echo/ & echo/ 

                 !_bg! print f " Host" c " offline" b ":" a " %%I"

                 echo/ & echo/ <nul

                ) 

        )

      ) else ( 

       color c && echo/E R R O R: "%temp%\bg.exe" not found...

       2>nul >nul (del /q /f "%temp%\bg64" &  "%temp%\bg.exe" del /q /f "%temp%\bg.exe")

       goto :_end_of_file

      )

:_base64_to_exe

(echo/-----BEGIN CERTIFICATE-----
echo/TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4AtAnNIbgBTM0hVGhpcyBwcm9ncmFtIGNhbm5v
echo/dCBiZSBydW4gaW4gRE9TIG1vZGUuDQ0KJAAAAAAAAABQRQAATAECAAAAAAAAAAAAAAAAAOAADwMLAQYAAAAAAAAAAAAAAAAA1x0AAAAQAAAAIAAAAABAAAAQAAAAAgAA
echo/BAAAAAAAAAAEAAAAAAAAAAAw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AA
echo/9SIAAP0iAAAUIwAAKyMAADwjAABSIwAAZSMAAHojAAAAAAAAmSMAAAAAAACxIwAAAAAAAN8hAADpIQAA8yEAAPwhAAAFIgAADiIAABciAAAgIgAAKyIAADsiAAAAAAAA
echo/TyIAAGwiAACGIgAAoSIAALAiAADBIgAA0iIAAOYiAAD1IgAA/SIAABQjAAArIwAAPCMAAFIjAABlIwAAeiMAAAAAAACZIwAAAAAAALEjAAAAAAAAbXN2Y3J0LmRsbAAA
echo/AHB1dGNoYXIAAABzdHJ0b3VsAAAAbWVtY3B5AAAAX2tiaGl0AAAAX2dldGNoAAAAcHJpbnRmAAAAc3RyY21wAAAAaXN4ZGlnaXQAAABfX2dldG1haW5hcmdzAAAAZXhp
echo/dABrZXJuZWwzMi5kbGwAAABHZXRDb25zb2xlU2NyZWVuQnVmZmVySW5mbwAAAFNldENvbnNvbGVUZXh0QXR0cmlidXRlAAAAU2V0Q29uc29sZUN1cnNvclBvc2l0aW9u
echo/AAAAR2V0U3RkSGFuZGxlAAAAR2V0Q29uc29sZU1vZGUAAABTZXRDb25zb2xlTW9kZQAAAFJlYWRDb25zb2xlSW5wdXRBAAAAR2V0TG9jYWxUaW1lAAAAU2xlZXAAAABH
echo/ZXRDb25zb2xlQ3Vyc29ySW5mbwAAAFNldENvbnNvbGVDdXJzb3JJbmZvAAAAU2V0Q29uc29sZUZvbnQAAABXcml0ZUNvbnNvbGVPdXRwdXRBAAAAR2V0Q29uc29sZVdp
echo/bmRvdwAAAEdldENvbnNvbGVPdXRwdXRDUAAAAFNldENvbnNvbGVPdXRwdXRDUAB3aW5tbS5kbGwAAABQbGF5U291bmRBAHVzZXIzMi5kbGwAAABTaG93V2luZG93AAAA
echo/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
echo/-----END CERTIFICATE-----)>>"%temp%\bg.b64" && exit /b || echo/ Base64 to exe unsuccessful...

:_end_of_file

2>nul >nul  del /q /f !_bg!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.