将“tree / f / a”结果保存到支持unicode的文本文件中

我试图在Windows命令行中使用树命令来生成一个文本文件,列出目录的内容,但是当我pipe道输出unicode字符得到塞满。

这是我正在使用的命令:

tree /f /a > output.txt 

控制台窗口中的结果很好:

 \ --- Erikaszobája
         cover.jpg
         Erikaszobája.m3u
         KátaiTamás -  01Télvíz.ogg
         KátaiTamás -  02Zölderdõ.ogg
         KátaiTamás -  03 Renoir kertje.ogg
         KátaiTamás -  04Esõbenszaladtál.ogg
         KátaiTamás -  05Ázikazút.ogg
         KátaiTamás -  06Sûrûvölgyektakaród.ogg
         KátaiTamás -  07Õszhozó.ogg
         KátaiTamás -  08Mécsvilág.ogg
         KátaiTamás -  09Zúzmara.ogg

但是文本文件不好:

 \ --- Erika szob ja
         cover.jpg
         Erika szob ja.m3u
         K tai Tam s  -  01T,lv¡z.ogg
         K tai Tam s  -  02 Z“lderdä.ogg
         K tai Tam s  -  03 Renoir kertje.ogg
         K tai Tam s  -  04Esäbenszaladt l.ogg
         K tai Tam s  -  05μzikaz£t.ogg
         K tai Tam s  -  06 S-r- v“lgyek takar¢d .ogg
         K tai Tam s  -  07Åszhoz.ogg
         K tai Tam s  -  08 M,csvil g.ogg
         K tai Tam s  -  09 Z£zmara.ogg

我怎样才能解决这个问题? 理想情况下,文本文件将与控制台窗口中的输出完全相同。

我尝试了Chris Jester-Young的build议(发生了什么事,你是否删除了Chris?),使用/ U开关运行命令行,看起来正是我所需要的,但看起来不起作用。 我曾尝试在VS2008和记事本中打开文件,并显示相同的不正确的字符。

有人已经试过这个:

 tree /f /a |clip 

打开记事本,按Ctrl + V,保存在记事本作为output.txt与Unicode支持?

如果输出为非Unicode(显然是这样),则必须使用Console窗口使用的相同编码查看您创建的文本文件。 这就是为什么它在控制台中看起来是正确的。 在某些文本编辑器中,您可以在打开文件时选择编码(或“代码页”)。 (如何输出为Unicode我不知道。cmd / U没有做文档说的。)

控制台编码取决于您的Windows安装。 对我来说,在Microsoft Word中是“西欧(DOS)”(或者就是“MS-DOS”)。

使用PowerShell:

 powershell -command "tree /f > tree.txt" 

测试用例:

create.ps1

 mkdir "Erika szobája" $null | Set-Content "Erika szobája/cover.jpg" $null | Set-Content "Erika szobája/Erika szobája.m3u" $null | Set-Content "Erika szobája/Kátai Tamás - 01 Télvíz.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 02 Zölderdõ.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 03 Renoir kertje.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 04 Esõben szaladtál.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 05 Ázik az út.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 06 Sûrû völgyek takaród.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 07 Õszhozó.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 08 Mécsvilág.ogg" $null | Set-Content "Erika szobája/Kátai Tamás - 09 Zúzmara.ogg" 

输出:

tree.txt

 Folder PATH listing Volume serial number is 00000000 0000:0000 C:. │ create.ps1 │ tree.txt │ └───Erika szobája cover.jpg Erika szobája.m3u Kátai Tamás - 01 Télvíz.ogg Kátai Tamás - 02 Zölderdo.ogg Kátai Tamás - 03 Renoir kertje.ogg Kátai Tamás - 04 Esoben szaladtál.ogg Kátai Tamás - 05 Azik az út.ogg Kátai Tamás - 06 Sûrû völgyek takaród.ogg Kátai Tamás - 07 Oszhozó.ogg Kátai Tamás - 08 Mécsvilág.ogg Kátai Tamás - 09 Zúzmara.ogg 

编辑:

针对高级用户的增强版和改进版

列表树菜单使用Unicode

测试用例:

 $null | Set-Content "欲速则不达.txt" $null | Set-Content "爱不是占有,是欣赏.txt" $null | Set-Content "您先请是礼貌.txt" $null | Set-Content "萝卜青菜,各有所爱.txt" $null | Set-Content "广交友,无深交.txt" $null | Set-Content "一见钟情.txt" $null | Set-Content "山雨欲来风满楼.txt" $null | Set-Content "悪妻は百年の不作。.txt" $null | Set-Content "残り物には福がある。.txt" $null | Set-Content "虎穴に入らずんば虎子を得ず。.txt" $null | Set-Content "夏炉冬扇.txt" $null | Set-Content "花鳥風月.txt" $null | Set-Content "起死回生.txt" $null | Set-Content "自業自得.txt" $null | Set-Content "아는 길도 물어가라.txt" $null | Set-Content "빈 수레가 요란하다.txt" $null | Set-Content "방귀뀐 놈이 성낸다.txt" $null | Set-Content "뜻이 있는 곳에 길이 있다.txt" $null | Set-Content "콩 심은데 콩나고, 팥 심은데 팥난다.txt" 

从他的回答中 ,@Chris Jester-Young写道:

现在,在ulibWriteString方法在两个类SCREENSTREAMSCREEN版本直接使用WriteConsoleW ,所以所有的Unicode字符都能正确显示。 STREAM版本将Unicode文本转换为三种不同编码( _UseConsoleConversions控制台代码页( GetConsoleCP ), _UseAnsiConversions默认ANSI代码页,否则⇒默认OEM代码页)中的一种,然后将其写入。

这意味着我们不能依靠从流中获取字符。 文件重定向不起作用。 我们必须依靠写入控制台来获取Unicode字符。

解决方法或破解是将树写入控制台,然后将缓冲区转储到文件。

我已经编写脚本来添加树上下文菜单,当你右键单击资源管理器中的目录。 将文件保存在同一目录下,然后以管理员身份运行Install list menu.bat进行安装。

Install list menu.bat

 @echo on regedit /s "List files.reg" copy "List.ps1" "%SystemRoot%" pause 

List files.reg

 Windows Registry Editor Version 5.00 ; Directory. [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Shell\List] "MUIVerb"="List" "ExtendedSubCommandsKey"="Directory\\ContextMenus\\List" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\Files] "MUIVerb"="Files" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\Files\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'files' -directory '%1' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,66,00,69,00,6c,00,65,00,73,00,27,00,20,00,2d,00,\ 64,00,69,00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,31,\ 00,27,00,00,00 [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\FilesRecursively] "MUIVerb"="Files recursively" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\FilesRecursively\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'filesRecursively' -directory '%1' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,66,00,69,00,6c,00,65,00,73,00,52,00,65,00,63,00,\ 75,00,72,00,73,00,69,00,76,00,65,00,6c,00,79,00,27,00,20,00,2d,00,64,00,69,\ 00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,31,00,27,00,\ 00,00 [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\Tree] "MUIVerb"="Tree" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\ContextMenus\List\Shell\Tree\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'tree' -directory '%1' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,74,00,72,00,65,00,65,00,27,00,20,00,2d,00,64,00,\ 69,00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,31,00,27,\ 00,00,00 ; Directory background. [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\Shell\List] "MUIVerb"="List" "ExtendedSubCommandsKey"="Directory\\Background\\ContextMenus\\List" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\Files] "MUIVerb"="Files" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\Files\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'files' -directory '%V' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,66,00,69,00,6c,00,65,00,73,00,27,00,20,00,2d,00,\ 64,00,69,00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,56,\ 00,27,00,00,00 [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\FilesRecursively] "MUIVerb"="Files recursively" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\FilesRecursively\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'filesRecursively' -directory '%V' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,66,00,69,00,6c,00,65,00,73,00,52,00,65,00,63,00,\ 75,00,72,00,73,00,69,00,76,00,65,00,6c,00,79,00,27,00,20,00,2d,00,64,00,69,\ 00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,56,00,27,00,\ 00,00 [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\Tree] "MUIVerb"="Tree" [HKEY_LOCAL_MACHINE\Software\Classes\Directory\Background\ContextMenus\List\Shell\Tree\Command] ; powershell -executionPolicy bypass "%SystemRoot%\List.ps1" -type 'tree' -directory '%V' @=hex(2):70,00,6f,00,77,00,65,00,72,00,73,00,68,00,65,00,6c,00,6c,00,20,00,2d,\ 00,65,00,78,00,65,00,63,00,75,00,74,00,69,00,6f,00,6e,00,50,00,6f,00,6c,00,\ 69,00,63,00,79,00,20,00,62,00,79,00,70,00,61,00,73,00,73,00,20,00,22,00,25,\ 00,53,00,79,00,73,00,74,00,65,00,6d,00,52,00,6f,00,6f,00,74,00,25,00,5c,00,\ 4c,00,69,00,73,00,74,00,2e,00,70,00,73,00,31,00,22,00,20,00,2d,00,74,00,79,\ 00,70,00,65,00,20,00,27,00,74,00,72,00,65,00,65,00,27,00,20,00,2d,00,64,00,\ 69,00,72,00,65,00,63,00,74,00,6f,00,72,00,79,00,20,00,27,00,25,00,56,00,27,\ 00,00,00 

List.ps1

 function sortNaturally { [Regex]::replace($_, '\d+', { $args[0].value.padLeft(20) }) } function writeList { param( [parameter(mandatory = $true)] [string] $text = $null ) $filePath = "$env:temp\List.txt" $text > "$filePath" notepad "$filePath" | out-null del "$filePath" } function listFiles { param( [switch] $recurse = $false ) get-childItem -name -recurse:$recurse -force | sort-object $function:sortNaturally | out-string } function listTree { tree /f } function getBufferText { $rawUi = $host.ui.rawUi $width = [Math]::max([Math]::max($rawUi.bufferSize.width, $rawUi.windowSize.width) - 1, 0) $height = [Math]::max($rawUi.cursorPosition.y - 1, 0) $lines = new-object System.Text.StringBuilder $characters = new-object System.Text.StringBuilder for ($h = 0; $h -lt $height; $h += 1) { $rectangle = new-object System.Management.Automation.Host.Rectangle 0, $h, $width, $h $buffer = $rawUi.getBufferContents($rectangle) for ($w = 0; $w -lt $width; $w += 1) { $cell = $buffer[0, $w] $character = $cell.character $characters.append($character) | out-null } $lines.appendLine($characters.toString()) | out-null $characters.length = 0 } $lines.toString() -replace '[ \0]*\r?\n', "`r`n" } function main { param( [parameter(mandatory = $true)] [string] $type = $null, [parameter(mandatory = $true)] [string] $directory = $null ) $outputEncoding = [Text.UTF8Encoding]::UTF8 [Console]::outputEncoding = [Text.UTF8Encoding]::UTF8 $PSDefaultParameterValues['out-file:encoding'] = 'utf8' set-location -literalPath "$directory" $typeFunction = @{ 'files' = { writeList -text $(listFiles) }; 'filesRecursively' = { writeList -text $(listFiles -recurse) }; 'tree' = { listTree writeList -text $(getBufferText) } } &($typeFunction.get_item($type)) } main @args 

我决定我必须看看tree.com并找出为什么不考虑控制台的Unicode设置。 事实证明(和许多命令行文件实用程序一样),它使用名为ulib.dll的库来执行所有打印(具体来说, TREE::DisplayNameulib调用WriteString )。

现在,在ulibWriteString方法在两个类SCREENSTREAMSCREEN版本直接使用WriteConsoleW ,所以所有的Unicode字符都能正确显示。 STREAM版本将Unicode文本转换为三种不同编码( _UseConsoleConversions控制台代码页( GetConsoleCP ), _UseAnsiConversions默认ANSI代码页,否则⇒默认OEM代码页)中的一种,然后将其写入。 我不知道如何改变转换模式,我不相信转换可以被禁用。

我只是简单地看了一下,所以也许更有冒险精神的人可以多说一些。 🙂

简短的答案是你不能 ,这是因为tree.com是一个ANSI应用程序,即使在Windows 7上。

唯一的解决方案是编写自己的tree实现。 你也可以向微软提交一个bug,但是我怀疑他们还没有意识到这一点。

这将在您的桌面上将结果保存为ASCII(美国信息交换标准代码),ASCII \ ANSI不能识别每个国际字符或扩展字符:

 tree /f > ascii.txt 

这会将您的ASCII文本转换为Unicode(/ c必须在实际命令之前):

 cmd /u /c type ascii.txt > unicode.txt 

那么为什么不把ascii文件当作临时文件来删除呢?

 del ascii.txt 

如果您必须将所有内容放在一行中,您可以使用:

 tree /f > ascii.txt & cmd.exe /u /c type ascii.txt > unicode.txt & del ascii.txt 

这对我工作:

 tree /f /a > %temp%\Listing >> files.txt 

你可以试试

 tree /A > output.txt 

虽然看起来与CMD线有所不同,但还是可以接受的。 :P

通过输出到控制台(只是tree ),然后从它复制(系统菜单 – >编辑 – >标记,选择所有,输入到控制台,所有非ASCII字符未转换,我已经成功获取输出)。 在控制台的属性(系统菜单 – >属性)中,控制台缓冲区大小应该预先增加,具体取决于数字文件/文件夹。 其他方式不起作用。 在之前的文章中提到的tree|clip将非ASCII字符转换为与tree>file.txt相同的ASCII字符。

我设法通过Take命令控制台正确输出树命令中的非ASCII字符到文件中。

在TCC类型的“选项”中,在第一个选项卡上选择“Unicode输出”。 然后简单地运行

 tree /f /a > output.txt