Saving 'tree /f /a" results to a textfile with unicode support

前端 未结 12 1694
清歌不尽
清歌不尽 2021-02-02 07:10

I\'m trying to use the tree command in a windows commandline to generate a text file listing the contents of a directory but when I pipe the output the unicode characters get st

相关标签:
12条回答
  • 2021-02-02 07:49

    I've succeeded getting the output as it is in console, with all non-ascii characters not converted, by outputting to the console (just tree) and then copying from it (system menu -> Edit -> Mark, selecting all, Enter). Console buffer size should be increased in advance, depending on number files/folders, in the console's properties (system menu -> Properties). Other ways didn't work. tree|clip, mentioned in an earlier post, converts non-ascii characters to ascii ones the same as tree>file.txt.

    0 讨论(0)
  • 2021-02-02 07:53

    I've managed to properly output non-ascii characters from tree command into a file via Take Command Console.

    In TCC type "option" and on first tab select "Unicode output". Then simply run

    tree /f /a > output.txt
    
    0 讨论(0)
  • 2021-02-02 07:54

    Have someone already tried this:

    tree /f /a |clip
    

    Open notepad, ctrl + V, save in notepad as output.txt with unicode support?

    0 讨论(0)
  • 2021-02-02 07:54

    If you output as non-Unicode (which you apparently do), you have to view the text file you create using the same encoding the Console window uses. That's why it looks correct in the console. In some text editors, you can choose an encoding (or "code page") when you open a file. (How to output as Unicode I don't know. cmd /U doesn't do what the documentation says.)

    The Console encoding depends on your Windows installation. For me, it's "Western European (DOS)" (or just "MS-DOS") in Microsoft Word.

    0 讨论(0)
  • 2021-02-02 07:54

    XP1's answer is great, but had a minor caveat: the output encoding is UCS2-LE, while I'd prefer UTF8 (smaller filesize, and more widespread).

    After a lot of searching and head scratching, I can finally present you the following command, that produces an UTF8-BOM file:

    PowerShell -Command "TREE /F | Out-File output.txt -Encoding utf8"
    

    If the output filename has spaces:

    PowerShell -Command "TREE /F | Out-File ""output file.txt"" -Encoding utf8"
    

    Many thanks to this article: https://www.kongsli.net/2012/04/20/powershell-gotchas-redirect-to-file-encodes-in-unicode/


    Also, personally I have created the following files in my PATH:

    xtree.cmd:

    @IF [%1]==[] @(
        ECHO You have to specify an output file.
        GOTO :EOF
    )
    
    @PowerShell -Command "TREE | Out-File %1 -Encoding utf8"
    

    xtreef.cmd:

    @IF [%1]==[] @(
        ECHO You have to specify an output file.
        GOTO :EOF
    )
    
    @PowerShell -Command "TREE /F | Out-File %1 -Encoding utf8"
    

    Finally, instead of tree > output.txt I just do xtree output.txt

    0 讨论(0)
  • 2021-02-02 07:58

    The short answer is you cannot and this is because tree.com is an ANSI application, even on Windows 7.

    The only solution is to write your own tree implementation. Also you could file a bug to Microsoft, but I doubt they are not already aware about it.

    0 讨论(0)
提交回复
热议问题