6

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?

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.