Menu with colored lines

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Menu with colored lines

#1 Post by Docfxit » 29 Sep 2022 21:53

I created a menu and I wanted the lines to be in color. I have two notices that I wanted in a different color.

When I added the coloring to one line it calls a routine at the bottom. I tried to return to the next line after the call.
I can't figure out why it won't return to the next line.

NOTE: Opening this menu is when you will see It's not working correctly
NOTE: WARNING Don't run options 2 or 3. It will remove Bitlocker from your harddrive.

Code: Select all

ECHO OFF
CLS
:: Remove Bitlocker
::     Run as Administrator
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"
)
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.
call :colorEcho a0 "1 - List BitLocker status"

call :colorEcho a0 "2 - Decrypt BitLocker on Drive C"

call :colorEcho a0 "3 - Decrypt BitLocker on Drive D"

ECHO 4 - EXIT
ECHO.

CHOICE /C:1234
IF ERRORLEVEL 1 SET M=1
IF ERRORLEVEL 2 SET M=2
IF ERRORLEVEL 3 SET M=3
IF ERRORLEVEL 4 SET M=4
IF %M%==1 GOTO Status
IF %M%==2 GOTO DecriptC:
IF %M%==3 GOTO DecriptD:
IF %M%==4 GOTO EOF

:Status
:: To check status of Bitlocker
manage-bde -status
GOTO MENU

:DecriptC:
::manage-bde -off C:
echo.
call :colorEcho 0a "Wait for this to complete...!"
echo.

GOTO MENU

:DecriptD:
::manage-bde -off D:
call :colorEcho 0a "Wait for this to complete...!"
GOTO MENU

:colorEcho
echo off
<nul set /p ".=%DEL%" > "%~2"
findstr /v /a:%1 /R "^$" "%~2" nul
del "%~2" > nul 2>&1i
GOTO :EOF
Last edited by Docfxit on 30 Sep 2022 11:02, edited 3 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Menu with colored lines

#2 Post by aGerman » 30 Sep 2022 01:13

CALLing a label is like the execution of a function. It automatically returns to the caller when it is finished. You have to replace your GOTOs at the end of the subroutine with either GOTO :EOF or EXIT /B though.

Steffen

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Menu with colored lines

#3 Post by Docfxit » 30 Sep 2022 10:19

Thank you for the reply...

I replaced the GOTO Return%ColorEcho% at the end of the Called subroutine with GOTO :EOF.

That didn't fix the problem. I originally had nothing there. It wouldn't return.

The error I am getting is "FINDSTR: Cannot open"

I just discovered calling the bat file that just opens the menu without selecting any options:
writes out two files:
2 - Decrypt BitLocker on Drive C
3 - Decrypt BitLocker on Drive D

Note: I am running the bat file Run as Administrator

I figured out the subroutine is writing out a file with the same name as the text:
when it writes out a file called:
"2 - Decrypt BitLocker on Drive C:"
The : at the end isn't a valid dos character so it writes:
"2 - Decrypt BitLocker on Drive C"
Then it tries to delete the file:
"2 - Decrypt BitLocker on Drive C:" with the colon. And the file isn't found.
I removed the colon which solved the "FINDSTR: Cannot open" problem.

Now it's outputting all three line on one line.

Thank you,

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Menu with colored lines

#4 Post by aGerman » 30 Sep 2022 11:25

You just used a very early version of this subroutine. Later versions are able to print any character. I don't recall which thread has been the one with the recent version. However, my edition of it is:

Code: Select all

@echo off

call :initColorPrint

call :colorPrint 0c " :-\ "
call :colorPrint 0e " :-) "
call :colorPrint 0a " :-D "
echo(

call :cleanupColorPrint

pause
exit /b

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:initColorPrint
for /f %%a in ('"prompt $H&for %%b in (:) do rem"') do set "-=%%a %%a"
<nul >"%temp%\~" set /p "=%-%%-%%-%%-%%-%%-%.%-%"&exit /b

:colorPrint  Color  Str
setlocal DisableDelayedExpansion
set "str=%~2"&call :colorPrintVar %1 str&endlocal&exit /b

:colorPrintVar  Color  StrVar
if not defined %~2 exit /b
setlocal EnableDelayedExpansion&set "str=a%-%!%~2:\=a%-%\..\%-%%-%%-%!"&set "str=!str:/=a%-%/..\%-%%-%%-%!"&pushd "%temp%"&findstr.exe /p /A:%1 . "!str:"=\"!\..\~" nul&popd&endlocal&exit /b

:cleanupColorPrint
del "%temp%\~"&exit /b
As you can see neither colons nor backslashes cause an issue.

Steffen

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Menu with colored lines

#5 Post by Docfxit » 30 Sep 2022 11:54

Thank you for your version.

I tried to adapt my menu to your version.
It works great

Code: Select all

ECHO OFF
CLS
:: Remove Bitlocker
::     Run as Administrator
call :initColorPrint
:: call :colorPrint 0c "Red Text with black backround"
:: call :colorPrint 0a "Black text with green backround"
:: call :colorPrint 0e "White with black backround"
:MENU
ECHO.
ECHO ...............................................
ECHO PRESS 1, 2 OR 3 to select your task, or 4 to EXIT.
ECHO ...............................................
ECHO.

call :colorPrint a0 "1 - List BitLocker status" 
echo.
call :colorPrint a0 "2 - Decrypt BitLocker on Drive C"
echo.
call :colorPrint a0 "3 - Decrypt BitLocker on Drive D"
echo.
ECHO 4 - EXIT
ECHO.

CHOICE /C:1234
IF ERRORLEVEL 1 SET M=1
IF ERRORLEVEL 2 SET M=2
IF ERRORLEVEL 3 SET M=3
IF ERRORLEVEL 4 SET M=4
IF %M%==1 GOTO Status
IF %M%==2 GOTO DecriptC:
IF %M%==3 GOTO DecriptD:
IF %M%==4 call :cleanupColorPrint&EXIT /B


:Status
:: To check status of Bitlocker
manage-bde -status
GOTO MENU

:DecriptC:
manage-bde -off C:
echo.
call :colorPrint 0a "Wait for this to complete...!"
echo.

GOTO MENU

:DecriptD:
manage-bde -off D:
echo.
call :colorPrint 0a "Wait for this to complete...!"
echo.
GOTO MENU

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:initColorPrint
for /f %%a in ('"prompt $H&for %%b in (:) do rem"') do set "-=%%a %%a"
<nul >"%temp%\~" set /p "=%-%%-%%-%%-%%-%%-%.%-%"&exit /b

:colorPrint  Color  Str
setlocal DisableDelayedExpansion
set "str=%~2"&call :colorPrintVar %1 str&endlocal&exit /b

:colorPrintVar  Color  StrVar
if not defined %~2 exit /b
setlocal EnableDelayedExpansion&set "str=a%-%!%~2:\=a%-%\..\%-%%-%%-%!"&set "str=!str:/=a%-%/..\%-%%-%%-%!"&pushd "%temp%"&findstr.exe /p /A:%1 . "!str:"=\"!\..\~" nul&popd&endlocal&exit /b

:cleanupColorPrint
del "%temp%\~"&exit /b
Last edited by Docfxit on 30 Sep 2022 12:40, edited 3 times in total.

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Menu with colored lines

#6 Post by aGerman » 30 Sep 2022 12:29

You know that your main code has to end with a GOTO :EOF or an EXIT /B as well? Otherwise it is going to run into the subroutines without calling them.

FWIW I'm wondering why you have chosen this old findstr technique. Win 10 onwards supports Ansi escape sequences for all kind of formatting, cursor positioning and stuff.
viewtopic.php?p=65991

Steffen

Docfxit
Posts: 132
Joined: 12 Nov 2015 12:42

Re: Menu with colored lines

#7 Post by Docfxit » 30 Sep 2022 12:49

On my Exit option I added:

IF %M%==4 call :cleanupColorPrint&EXIT /B

That fixed the problem.

The reason I started working with the findstr technique is because that's the first thing I found when I did a search for Batch Menu select.

It looks like the new way has a lot more functionality. At this point I really don't need it. Your findstr technique is working great for me.

Thank you very much for all your help...

Docfxit

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Menu with colored lines

#8 Post by aGerman » 30 Sep 2022 13:38

It's so much easier and faster though.

Code: Select all

@echo off
for /f %%i in ('echo prompt $E^|cmd') do set "esc=%%i"

echo %esc%[92;40mHello Green World%esc%[0m

pause
Steffen

Post Reply