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

1

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 was normal, the letters of the cmd would be green, if it went without a connection it would change to red ...

    
asked by anonymous 16.04.2016 / 21:07

3 answers

3

Note that to change the color individually per line, you need some help.

The rest is basically this:

//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 case the color of the entire window changes, to make individual lines of different colors, it is necessary to change the COLOR by some external utility.

Obviously it's just a sketch, but it already has the complete logic for you to adapt as you see fit.


Coloring the Output

Here is a utility that makes% color:%

  

link

Follow the code tailored for colored lines separately, using the utility mentioned:

@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
    
16.04.2016 / 23:04
0

Use '' if '', see examples:

if exist %SysPath%\%FileName% takeown /f %SysPath%\%FileName% >nul && icacls %SysPath%\%FileName% /grant *S-1-5-32-544:F >nul
if exist %SysPath%\%FileName% ren %SysPath%\%FileName% %FileName%.vvv >nul
if exist %SysPath%\%FileName% >nul move %SysPath%\%FileName% %Temp%\%Random%.tmp >nul
ping 127.0.0.1 -n 3 >nul
copy /y x64\%FileName% %SysPath%\%FileName% >nul
ping 127.0.0.1 -n 3 >nul
if exist %SysPath%\%FileName%.vvv move %SysPath%\%FileName%.vvv %Temp%\%Random%.tmp >nul
set /A i=i+1
if /i %i% == 1 set FileName=GenuineCenter.dll&& goto:fn64
if /i %i% == 2 set FileName=shell32.dll&& goto:fn64
if /i %i% == 3 set FileName=slmgr.vbs&& goto:fn64
if /i %i% == 4 set FileName=slwga.dll&& goto:fn64
if /i %i% == 5 set FileName=twinui.dll&& goto:fn64
if /i %i% == 6 set FileName=Windows.UI.Immersive.dll&& goto:fn64

call %SysPath%\regedt32.exe /s Activation.reg
goto _end

:_end
if not exist %Windir%\Temp md %Windir%\Temp
copy /y Rebooting.cmd %Windir%\Temp\Rebooting.cmd
cd /d %Windir%\Temp
call Rebooting.cmd
takeown /f "%Systemdrive%\Windows Activation Technologies\data" /r /d y >nul && icacls "%Systemdrive%\Windows Activation Technologies\data" /t /deny *S-1-5-32-544:F >nul
exit
    
16.04.2016 / 22:47
0

Somethinglikethis?

@echooff&&setlocalenableextensionsenabledelayedexpansion(ifexist"%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...

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

       goto :_end_of_file

      )

:_base64_to_exe

(echo/-----BEGIN CERTIFICATE-----
echo/TVqQAAMAAAAEAAAA//8AALgAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAA4fug4At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QbGF5U291bmRBAHVzZXIzMi5kbGwAAABTaG93V2luZG93AAAA
echo/AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==
echo/-----END CERTIFICATE-----)>>"%temp%\bg.b64" && exit /b || echo/ Base64 to exe unsuccessful...

:_end_of_file

if exist !_bg! del /q /f !_bg!
if exist !_bg! del /q /f !_bg!
    
30.11.2018 / 00:24