Windows batch – how to save terminal output whilst preserving colored text

colorsterminalwindows

Sometimes the program output in terminal is colored. For example aria2:

aria2c https://superuser.com > z.txt

The output file looks like this:

08/13 03:42:27 [[1;32mNOTICE[0m] Downloading 1 item(s)

08/13 03:42:28 [[1;32mNOTICE[0m] Download complete: G:/LH/1/index.html

The output of the command (without redirecting output to file), looks like this:
enter image description here

I noticed in Mac OS it is possible to show the output with colors using cat.

Also in Linux it is possible to do that in GNOME Terminal

I tried with type and cat (from GnuWin32) and it doesn't work.

Is there any solution for this? I have sed, I can use it to replace the ASCII escape code (Hex 1B) into something like "^ESC^"

Best Answer

aria2c did save the colors – the problem is that you cannot view them in your version of Windows.

Both macOS and Linux (i.e. GNOME Terminal) use formatting through "ANSI sequences" that are output intermixed with the actual text – e.g. the ESC [1;32m in your aria2c log means "bold & green".

But the Windows console did not, for a very long time. If a program wanted to use colors in console windows it had to call special Windows APIs to make that happen. If the program just wrote ANSI sequences to its output – Windows wouldn't care. (Overall there's actually a reason it gets called a "console window" instead of a "terminal window".)

Console ANSI sequence support was only added in Windows 10 – first in version 1511, then disabled in 1607, then enabled again in 1909. It looks like your screenshot is from Windows 8.1 (or Server 2012, also based on 8.1) so you do not have this feature available yet.

In your case the color information was saved as ANSI sequences, though, as aria2c automatically switches between "Windows console API" and "ANSI" modes – it uses the APIs when writing to console, but writes ANSI sequences when it detects its output is going a file. But you won't be able to view it in Windows 8 with just type; instead you'll need to:

  • Upgrade to Windows 10. The built-in console has ANSI support in Windows 10, although Microsoft's "Windows Terminal" app is much better overall.

  • Download another console app which does understand VT sequences – it seems ConEmu is a popular choice for Cmd.exe users.

    Installing Xterm/Urxvt/GNOME Terminal via Cygwin is also an option. If you have "Git for Windows" installed, it comes with MinTTY which would work in this case as well.

  • Read the log file through SSH (e.g. PuTTY). Even Windows SSH clients usually come together with an ANSI/VT compatible terminal emulator.

  • Install the ANSICON hack which adds ANSI sequence support through hooks.

  • Read the log file through an "ANSI art viewer". Those are meant to be compatible with MS-DOS ANSI.SYS, so they often support a limited set of formatting sequences (usually 8 colors only) and completely lack Unicode support, but they'll do in a pinch.

  • Use the Cygwin version of 'cat' – the cygwin runtime will automagically translate ANSI sequences output by cygwin-based programs into Windows Console API calls as necessary.

Related Question