0

I know that there are already answers to my question, but I'm dumb and don't know how to change the code in those answers to suit my needs, so please don't report this question, but rather help me with this problem.

Example code:

@echo off
color 0a
echo #######################################
echo ##                                   ##
echo ##     ########################      ##
echo ##     ## Password Protected ##      ##
echo ##     ########################      ##
echo ##                                   ##
echo #######################################
choice /c:ws /n /m "thisisjustrandomtext" 
if %ERRORLEVEL% == 1 goto testpart1
if %ERRORLEVEL% == 2 goto testpart2

:testpart1
echo thisispart1
pause >NUL
exit

:testpart2
echo thisispart2
pause >NUL
exit

How do i make the case of the "password protected" sign IE:

########################
##                    ##
########################

be red, but have everything else be green.

  • 3
    It can be done but is not easy and if I were to post n answer and you delete is what do I get for my time? Free code requests are not supported here unfortunately – Gerhard Feb 12 '19 at 19:29
  • edited the question, sorry. – k9-primetime Feb 12 '19 at 19:49
  • 2
    Which Operating System is this for? And no, we will not design and make a template to prevent you from ever bothering to learn, understand or attempt to create further scripts for yourself in future. Please use the other answers you've found, and make your best attempt at a solution, or just forget about it, _because after all there is no need to make multi-colored text in cmd.exe_. – Compo Feb 12 '19 at 19:59
  • It's for Windows 7, and I'm making a game for fun and would like to have multiple colors... – k9-primetime Feb 12 '19 at 20:31
  • 1
    The basic color function is posted as an answer below. But if you need something more to handle special characters I would advise you to use this one at [DosTips.com](https://www.dostips.com/forum/viewtopic.php?p=41155#p41155) – Squashman Feb 12 '19 at 20:36
  • 1
    see [this question](https://stackoverflow.com/questions/4339649/how-to-have-multiple-colors-in-a-windows-batch-file). If it doesn't help you, see the linked questions there for more stuff. – Stephan Feb 12 '19 at 20:51
  • At least you had the foresight to delete your `I'm really desperate` statement from your question 35 minutes after adding a comment like, `I'm making a game for fun`! – Compo Feb 12 '19 at 21:27

2 Answers2

1

Here is something I ammended a bit, nothing too fancy, just showing you what can be done. The original was from an answer here by Jeb, though really old, alot of changes can be made to improve it, but I am giving you what I can for now based on my time of day here:

@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 "clr=%%a"
)

echo/
call :change 4 #########################
echo/
call :change 4 "#     #"
call :change 2A "Some String"
call :change 4 "#     #"
echo/
call :change 4 #########################

goto :eof

:change
echo off
<nul set /p ".=%clr%" > "%~2"
findstr /v /a:%1 /r "^$" "%~2" nul
del "%~2" > nul 2>&1
Gerhard
  • 18,114
  • 5
  • 20
  • 38
  • first question: am i supposed to put my code in the middle, where the echo and call commands are? second question: i have never seen echo/, is that the same as echo. or is it something else? – k9-primetime Feb 12 '19 at 20:37
  • 1
    This is an example. do another echo before or after the calls, then run some commands to test, it is the easiest way you'll learn. :) – Gerhard Feb 12 '19 at 20:42
  • 1
    @k9-primetime, you should be able to tell from running the code and seeing the output which lines in the code is what is being output. – Squashman Feb 12 '19 at 20:44
  • that's what I'm trying to do right now – k9-primetime Feb 12 '19 at 21:00
  • Ok. Do that, test, try then ask if you get stuck by showing code, results and expected results. – Gerhard Feb 12 '19 at 21:08
  • i figured out some stuff, but I'm having trouble with having two colors in the same row, something like `echo ## & call :change 4 ######################### ##`, tryed incasing the hashtags with "" and similar, but nothing – k9-primetime Feb 12 '19 at 21:09
  • Hint. The `echo/` lines are echoing new lines. So do `call :change 1 ##` then next line `call :change 4 #########################` which will do 2 different colors in one line. Only if you add `echo/` in a new line will it echo newline. – Gerhard Feb 12 '19 at 21:22
  • i tried a couple of versions, but for some reason it doesn't work, pops a bunch of error messages, maybe it's my original code for the game that's interfering but I'm tired, ill try tomorrow and see what happens. – k9-primetime Feb 12 '19 at 22:19
  • Edit your question with the ammendes code you tried so I can see what is wrong and will help. – Gerhard Feb 13 '19 at 04:28
1

Make your own programs to do what you want.

Unlike the 13 stupid answers on the linked duplicate page. This will be blindly fast. CMD.exe opens a batchfile, reads 1 line, closes the file. It repeats for each line. Worse using variables means the environment block needs to be resorted each time.

@ColourText f2 F2 *********************************
@ColourText F4 F2 Warning
@Echo ***************************************

REM ColourText.bat
REM Compiles ColourText.vb to ColourText.exe
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\ColourText.exe" "%~dp0\ColourText.vb" /verbose
pause

'ColourText.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

Sub Main()
    Dim hOut as Long
    Dim Ret as Long
    Dim Colour As Long
    Dim Colour1 As Long
    Dim Text As String
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Colour = CLng("&h" & Split(Command(), " ")(0))
    Colour1 = Clng("&h" & Split(Command(), " ")(1))
    Text = Mid(Command(), 7)
    Ret = SetConsoleTextAttribute(hOut,  Colour)
    Console.Out.WriteLine(text)
    Ret = SetConsoleTextAttribute(hOut, Colour1)
End Sub
End Module

To use

ColourText <ColourOfText> <ColourOfTextWhenFinished> [Text]

Also the CLS command becomes interesting. Color command without parameters resets all colours to startup colours.

To get the colour code add the following numbers together. Use Calculator in programmers mode. These are hex numbers. They can be added together eg Red + Blue + FG Intensity = 13 = D. As 10+ wasn't used the background will be black. Colour codes MUST be two characters, eg 08 not 8.

FOREGROUND_RED = &H4     '  text color contains red.
FOREGROUND_INTENSITY = &H8     '  text color is intensified.
FOREGROUND_GREEN = &H2     '  text color contains green.
FOREGROUND_BLUE = &H1     '  text color contains blue.
BACKGROUND_BLUE = &H10    '  background color contains blue.
BACKGROUND_GREEN = &H20    '  background color contains green.
BACKGROUND_INTENSITY = &H80    '  background color is intensified.
BACKGROUND_RED = &H40    '  background color contains red.

So black backgroung is 0 while white is F0 (adding 10 + 20 + 40 + 80). Red on white is f4.

EDIT

This prints <GREEN>*<RED>Warning<GREEN>*

Use the right tool for the job.

'ColourText1.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication  
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

Sub Main()
    Dim hOut as IntPtr
    Dim Ret as Integer
    hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
    Ret = SetConsoleTextAttribute(hOut,  &hfA)
    Console.Out.Write("*")
    Ret = SetConsoleTextAttribute(hOut, &hfC)
    Console.Out.Write("Warning")
    Ret = SetConsoleTextAttribute(hOut, &hfA)
    Console.Out.Write("*" & vbcrlf)
End Sub
End Module
Noodles
  • 186
  • 1
  • 4