Command Prompt "tree" shows Unicode in console but outputs to file in ASCII

6

0

In Windows 10's cmd.exe, if I do

tree
C:.
├───a
│   ├───b
│   └───etc...

it clearly display the Unicode bars and dashes correctly on the console.

But if I redirect this output to a file

tree > tree.txt

the contents of the file are:

C:.
³ÄÄÄa
³   ÃÄÄÄb
³   ÀÄÄÄetc...

It fails to properly write the Unicode characters.

It can't even read back what it wrote:

tree > tree.txt
type tree.txt
C:.
����a
�   ����b
�   ����etc...

(it may appear as "�" here (question marks in a tilted square) but in the console they appear as empty boxes)

I've already tried these suggestions without luck:

Try chcp 1252 or chcp 65001 from the command line. With Lucida Console or other font support.

CMD /U /c tree > tree.txt

I was using this command incorrectly, now fixed, but I still get the same result as just tree - it displays fine in the console, but file output is still those weird characters.

It's strange that the text displays on the console just fine, but writing to file messes up the encoding. What could be the issue here?

laggingreflex

Posted 2016-03-23T21:21:01.627

Reputation: 3 498

2

Have a look to this other post related to the same issue

– Ludovic Frérot – 2016-03-23T21:41:50.097

Answers

2

The encoding that is generally used for the box-drawing characters is 850, but even that doesn't help on my Windows 8.1 system. I've only found one way around cmd's poor handling of text encodings: using a program that does handle encoding well, namely, PowerShell.

It's the > redirection that wrecks the characters, so we'll just suck up the tree output and jam it in a file without simple cmd redirection:

Invoke-Expression "tree" | Out-File "tree.txt"

Or, shorter:

iex "tree" > "tree.txt"

To create an instance of PowerShell that runs that command and then exits, use this in a command prompt or batch file:

powershell -command "iex \"tree\" > \"tree.txt\""

The resulting file can be viewed successfully in Notepad or by a normal type.

Ben N

Posted 2016-03-23T21:21:01.627

Reputation: 32 973

You may be interested in my answer for the same issue, on stackoverflow: https://stackoverflow.com/questions/138576/saving-tree-f-a-results-to-a-textfile-with-unicode-support/51255816#51255816 - It's the same technique, that I have improved to produce UTF8 instead of UCS2-LE.

– Gras Double – 2018-07-10T01:00:31.167