Page 1 of 1

How To find text and highlight in another colour

Posted: 03 Aug 2017 23:01
by NorfolkBatch
Hi my question to any batch developers is this, How can I search a command for example net user and highlight guest name in a chosen colour

Example

C:\Users\MyUser>net user

User accounts for \\Test

-------------------------------------------------------------------------------
Administrator DefaultAccount Guest
MyUser
The command completed successfully.

I found this code and played about a bit with it and maid a menu in different colour texts

Code: Select all

:A
@echo off
@Echo Off
SETLOCAL EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho A0 "1)Main Menu"
echo.
call :colorEcho C0 "2)Exit"
echo.
set /p op=Enter Command Here
if %op% ==1 goto 1
if %op% ==2 goto 2
:1
cls
net user
pause
cls
goto A
:2
exit
:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i


it works a dream only I need to find a way if possible to search for a given work and highlight that coulor in Green but not sure how or if it can be done.

Any advise is much appreciated.

Thank you for taking the time to read this post.

Kind Regards

Bradley

Re: How To find text and highlight in another colour

Posted: 06 Aug 2017 02:22
by penpen
This might help you (sketched; currently case insensitive):

Code: Select all

:A
@echo off
SETLOCAL enableDelayedExpansion EnableDelayedExpansion
for /F "tokens=1,2 delims=#" %%a in ('"prompt #$H#$E# & echo on & for %%b in (1) do     rem"') do (
  set "DEL=%%a"
)
call :colorEcho A0 "1)Main Menu"
echo.
call :colorEcho C0 "2)Exit"
echo.
set /p op=Enter Command Here
if %op% ==1 goto 1
if %op% ==2 goto 2

:1
cls
setlocal disableDelayedExpansion
set "keyword=Guest"
set "color=0A"
for /f "tokens=* delims=" %%a in ('net user') do (
   set "line="%%~a""
   setlocal enableDelayedExpansion
   set "line=!line:%keyword%=" "%keyword%" "!"
   for %%a in (!line!) do (
      if "%%~a" == "%keyword%" (
         call :colorEcho %color% "%%~a"
      ) else (
         <nul set /p "=#%DEL%%%~a"
      )
   )
   endlocal
   echo(
)
endlocal
pause
cls
goto A

:2
exit /b

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i

penpen

Re: How To find text and highlight in another colour

Posted: 06 Aug 2017 02:27
by NorfolkBatch
Thank You for your help I will give it a go

Re: How To find text and highlight in another colour

Posted: 06 Aug 2017 02:28
by NorfolkBatch
Works Perfectly good job thank you for your help.