How To find text and highlight in another colour

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
NorfolkBatch
Posts: 10
Joined: 03 Aug 2017 22:49

How To find text and highlight in another colour

#1 Post by NorfolkBatch » 03 Aug 2017 23:01

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

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How To find text and highlight in another colour

#2 Post by penpen » 06 Aug 2017 02:22

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

NorfolkBatch
Posts: 10
Joined: 03 Aug 2017 22:49

Re: How To find text and highlight in another colour

#3 Post by NorfolkBatch » 06 Aug 2017 02:27

Thank You for your help I will give it a go

NorfolkBatch
Posts: 10
Joined: 03 Aug 2017 22:49

Re: How To find text and highlight in another colour

#4 Post by NorfolkBatch » 06 Aug 2017 02:28

Works Perfectly good job thank you for your help.

Post Reply