批处理文件 – chcp 65001和.bat文件
我在
Windows shell中遇到chcp 65001命令的问题.
我需要生成一个文件夹中的文件列表. cd folder dir /B /O:N > list_of_files.txt 它工作,但我遇到了一些特殊的非ASCII字符的问题,这些字符在某些文件名中. 一切正常,但当我将这些命令放入.bat文件时,脚本不起作用. 所以 cd folder chcp 65001 dir /B /O:N > list_of_files.txt 不生成列表. 和 cd folder chcp 65001 && dir /B /O:N > list_of_files.txt 以及 cd folder chcp 65001 > nul && dir /B /O:N > list_of_files.txt 生成列表,但使用默认编码:/. 一切都在cmd.exe中,但不在.bat文件中. 我已经阅读了主题:stackoverflow.com/questions/2182568/batch-script-is-not-executed-if-chcp-was-called,但它没有帮助. 编辑:
使用cmd / U.见
http://ss64.com/nt/cmd.html:
这是我的尝试(当然是在cmd / A下启动): @ECHO OFF >NUL SETLOCAL EnableExtensions :: create a UNICODE file with Byte Order Mark using `wmic` chcp 852 >NUL >list_of_files.txt wmic os get localdatetime :: store a line with BOM to a variable :: although FINDSTR does not support UTF-16 files :: it will read first three bytes at least for /F "delims=" %%G in (' findstr "^" list_of_files.txt ') do set "UTF8BOM=%%G" :: write BOM only* to a file (* echo writes hexadecimal value FFFE0D0A) :: the `<NUL set /p =text` trick does not work: chokes down leading `FF` >list_of_files.txt echo(%UTF8BOM:~0,2% chcp 65001 >NUL :: add CRLF in Unicode (hexadecimal 0D000A00) >>list_of_files.txt cmd /U /C echo( :: add result of `dir /B /O:N` in Unicode >>list_of_files.txt cmd /U /C dir /B /O:N :: check the result: still invalid first line,see output type list_of_files.txt chcp 852 >NUL 输出.仍然无效的第一行(即十六进制0D0A),对不起;使用另一种方法获得纯Utf-8字节顺序标记: ==>cmd /A /C D:batSOUTF8BOM32182619.bat ? cpANSI_OoCcSsUu.txt cpANSI_??????üü.txt escrzyaie.txt ě????yáíé.txt list_of_files.txt ==> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- windows-server-2008-r2 – 如何在Windows Server 2008 R2中
- Windows符号链接目标
- 击败Windows上的CIFS文件缓存/强制“刷新”
- 如何在Windows中开始编程?
- DropShadow for WPF无边界窗口
- windows-phone-8.1 – 提交给Windows Phone Store的错误版本
- windows-server-2012 – HP ProLiant MicroServer Gen 8 –
- windows – 部署期间未更新的环境变量
- 何时包含.lib以及何时包含.dll或两者
- 如何使用NFC从Windows Phone 8发送一个字符串到Android设备