Jump to content

Print text in color cmd.exe


Recommended Posts

Thank you UEZ

I ran the example and was given an error that $FOREGROUND_RED variable was not defined so I changed it to $COLOR_1 which printed the text in color.

#include <Console.au3>

Main()

Func Main()
    Local $Name
    Local $Age
    Local $Answer
    Local $Continue = True

    While $Continue
        Cout("Enter your name: ")
        Cin($Name)
        Cout("Enter your age: ")
        Cin($Age)
        Cout("Do you want your answers printed in red? y/n: ")
        Cin($Answer)
        If StringInStr($Answer,"y") Then
            Cout(@LF & "Your name is ")
            Cout($Name & @LF,$FOREGROUND_RED)
            Cout("You were born in ")
            Cout(@Year - $Age & @LF,$FOREGROUND_RED)
        Else
            Cout(@LF & "Your name is ")
            Cout($Name & @LF)
            Cout("You were born in ")
            Cout(@Year - $Age & @LF)
        EndIf
        $Answer = ""
        Cout(@LF & "Do you want to try again? y/n: ")
        Cin($Answer)
        If StringInStr($Answer,"n") Then
            $Continue = False
        EndIf
    WEnd

    system("pause")
EndFunc

I would like this so that it stays native to the current command window it was run from and not having to spawn a new console so I tried the below and compile as a console application and ran it from the command console.

#include <Console.au3>


Cout($cmdline[1] & @LF, $COLOR_252)

But this prints the line in the default color and in the defined color. I just want it to print the line once and in the color I specify, how can I do this. Thank you again. :)

Edited by Cleptography
Link to comment
Share on other sites

I would like this so that it stays native to the current command window it was run from and not having to spawn a new console so I tried the below and compile as a console application and ran it from the command console.

But this prints the line in the default color and in the defined color. I just want it to print the line once and in the color I specify, how can I do this. Thank you again. :)

There are some nasty ways to do this in some .bat files BUT that is not very reliable. Also some 3rd party software can accomplish that.

My recomendation would rather be to create a GUI. If you like it can even look like a console/Command prompt.

Enter you commands from there and so control al the colour, input and output from there.

Link to comment
Share on other sites

  • 4 months later...

Hi all, I read this forum a while back when trying to get coloured text in cmd.exe and thought I'd post now that I have a solution. I eventually gave up searching, all I found was endless stuff about ANSI commands and the cmd.exe properties menu which didn't suit what I wanted. So, I made my own solution. It's a tiny exe (~7kb) called col.exe that can change the current cmd.exe text attributes. For example..

col -fg=ri

Will get you 'intense' (ie: fully saturated) red text. You can download it here: http://www.finalfury.co.uk/view_post.php?f=tools&id=122

and here's a print out of the full set of options available..

Col, by Chris Plant. http://www.finalfury.co.uk
Available options..
-fg=[rgbiu]
        Set the foreground via one or more of..
        r = red
        g = green
        b = blue
        i = intense
-bg=[rgbi]
        Set the background via one or more of..
        r = red
        g = green
        b = blue
        i = intense
-gr=[tlrb]
        Set the grid via one or more of..
        t = top grid line
        l = left grid line
        r = right grid line
        b = bottom grid line
-add
        Makes changes additive. By default they are absolute.
-praw
        Print the current attributes to stdout.
-graw
        Get the current attributes. Returned in the program
        exit code.
-sraw=<value>
        Set the current attributes from a raw decimal value.
Note: Run without any options to get white text on a black
background. Useful if you can't see what you're doing!
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...