- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
有什么命令可以知道windows中文件的编码?
比如一个文件 A.txt
编码是 UTF-16
最佳答案
在 Windows 命令提示符 (cmd
) 中,据我所知,没有任何命令能够确定文本文件的编码方式。
尽管如此,我写了一个小的批处理文件,它能够检查一些条件,从而确定给定的文本文件是 ASCII-/ANSI 编码还是 Unicode 编码(UTF-8 或 UTF-16,Little Endian或大字节序)。首先,它检查第一行(非空行)是否包含零字节,这表明该文件不是 ASCII-/ANSI 编码的。接下来,它检查前几个字节是否构成Byte Order Mark。 (BOM) 用于 UTF-8/UTF-16。由于 BOM 对于 Unicode 编码文件是可选的,因此它的缺失对于 ASCI/ANSI 编码文件来说并不是一个明确的标志。
下面是代码,其中包含大量解释性注释 (rem
) -- 希望对您有所帮助:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
rem // Define constants here:
set "_FILE=%~1" & rem // (provide file via the first command line argument)
rem // Check whether a dedicated file is given (so no wild-cards):
2> nul >&2 (< "%_FILE%" set /P ="" & ver) || (
rem // The file does not exist:
>&2 echo The file could not be found, hence there is no encoding!
exit /B 255
)
rem // Determine the file size:
set "SIZE=" & for %%F in ("%_FILE%") do set "SIZE=%%~zF"
if not defined SIZE (
rem // The file does not exist:
>&2 echo The file could not be found, hence there is no encoding!
exit /B 255
)
if %SIZE% EQU 0 (
rem // The file is empty:
>&2 echo The file is empty, hence encoding cannot be determined!
exit /B 1
)
rem // Store current code page to be able to restore it finally:
for /F "tokens=2 delims=:" %%C in ('chcp') do set /A "$CP=%%C"
rem /* Change to code page 437 (original IBM PC or DOS code page) temporarily;
rem this is necessary for extended characters not to be converted: */
> nul chcp 437
rem // Attempt to read first line from file; this fails if zero-bytes occur:
(
rem /* The loop does not iterate over an empty file or one with empty lines only;
rem therefore, the behaviour is the same as when zero-bytes occur: */
for /F usebackq^ delims^=^ eol^= %%L in ("%_FILE%") do (
rem // Abort reading file after first non-empty line:
goto :NEXT
)
) || (
rem /* The `for /F` loop returns a non-zero exit code in case the file is empty,
rem contains empty lines only or the first non-empty line contains zero-bytes;
rem to determine whether there are zero-bytes, let `find` process the file,
rem which removes zero-bytes or converts them to line-breaks, so `for /F` can
rem read the file;
rem however, `find` would read the whole file, hence do that only for small
rem ones and skip that for large ones, such contains zero-bytes most likely: */
if %SIZE% LEQ 8192 (
(
rem // In case the file contains line-breaks only, the loop does not iterate:
for /F delims^=^ eol^= %%L in ('^< "%_FILE%" find /V ""') do (
rem // Abort reading file after first non-empty line:
goto :ZERO
)
) || (
rem /* The loop did not iterate, so the file contains line-breaks only;
rem restore the initial code page prior to termination: */
> nul chcp %$CP%
>&2 echo The file holds only empty lines, hence encoding cannot be determined!
exit /B 1
)
)
)
rem // This point is reached in case the file contains zero-bytes:
:ZERO
rem // Restore the initial code page prior to termination:
> nul chcp %$CP%
>&2 echo NULL-bytes detected in first line, so file is non-ASCII/ANSI!
exit /B 2
rem // This point is reached in case the file does not contain any zero-bytes:
:NEXT
rem /* Build Byte Order Marks (BOMs) for UTF-16-encoded text (Little Endian and Big Endian)
rem and for UTF-8-encoded text: */
for /F "tokens=1-3" %%A in ('
forfiles /P "%~dp0." /M "%~nx0" /C "cmd /C echo 0xFF0xFE 0xFE0xFF 0xEF0xBB0xBF"
') do set "$LE=%%A" & set "$BE=%%B" & set "$U8=%%C"
rem /* Reset line string variable, then store first line string (1023 bytes at most);
rem in contrast to `for /F`, this does not skip over blank lines: */
< "%_FILE%" (set "LINE=" & set /P LINE="")
rem // Check whether the first line of the file begins with any of the BOMs:
if not "%LINE:~,2%"=="%$LE%" if not "%LINE:~,2%"=="%$BE%" if not "%LINE:~,3%"=="%$U8%" goto :CONT
rem /* One of the BOMs has been encountered, hence the file is Unicode-encoded;
rem restore the initial code page prior to termination: */
> nul chcp %$CP%
>&2 echo BOM encountered in first line, so file is non-ASCII/ANSI!
exit /B 4
rem // This point is reached in case the file does not appear as Unicode-encoded:
:CONT
rem // Restore the initial code page prior to termination:
> nul chcp %$CP%
echo The file appears to be an ASCII-/ANSI-encoded text.
endlocal
exit /B 0
关于cmd - 如何使用命令行知道文件的编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43136296/
我正在尝试制作一个命令行界面程序,它可以从用户输入中获取代码行并使用 execlp 执行它们。 我想知道是否有更好的方法来编写我的代码。 execlp(cmd[0], cmd[0], cmd[1] c
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
我使用此脚本查找当前文件夹及其 .bat 文件: for /f %%i in ("%0") do set curpath=%%~dpi echo %curpath% 如果路径包含空格,它无法正常工作
在 Windows 的命令提示符处,我可以通过键入 echo 来回显响铃字符,然后按住 Ctrl+G 生成 echo ^G,运行时会发出铃声。 当我实际用键盘输入 echo ^G 时,它只会在屏幕上打
这是我执行的命令: >cmd /k >echo 1 1 >echo 2 2 >echo 3 3 >exit /b >cmd /c "doskey /history" echo 1 echo 2 ech
我需要编写一个命令来更改当前目录并打印包含在一些标签中的 NEW 目录。我以为 cd SOMEPATH & echo wkd%cd%wkd会这样做,但有一个问题。 这是一些示例输入和输出 C:\Use
我正在尝试从“调用 ppm 查询断言”中捕获 stoutput,如果它等于“ * 没有安装匹配 'assert' ** 的软件包”或更好但包含字符串“无软件包”做“某事” “.. 正在安装软件包。任何
似乎一个cmd脚本包含: prog1 prog2 与...相同 call prog1 call prog2 使用CALL命令有什么意义? 最佳答案 当需要调用另一个批处理程序(cmd脚本)时,应使用c
我要打电话 cmd /c "C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\devenv.com" mysolution.sln /b
在 Windows 上,可以轻松以管理员身份运行 cmd 应用程序: Right click on cmd icon >> run as administrator` 但我想以管理员身份使用 PhpS
我目前正在使用 Python 的 cmd 模块创建一个基于命令的游戏。 在某个时刻,我的 cmd.Cmd 对象会被嵌套。如果我说我正在运行命令提示符 A,那么在某个时刻,会在 A 内创建一个新的提示符
我正在尝试构建一个 cli 框架,其中需要动态添加命令。我想要实现的是 - 我将有一个继承自 cmd.Cmd 的最小类,稍后我将在单独的类中编写命令并将这些命令与主类一起加载。 以下是我尝试过的,但在
以下命令有什么区别: start %comspec% /c script.cmd start cmd /C script.cmd 我需要 script.cmd 的 cmd 窗口在 script.cmd
我正在尝试将一个长而复杂的 Windows 批处理文件转换为 Python。 除了细微的问题外一切正常,我怀疑这与引用有关,但不太清楚。 在批处理文件中,这工作正常: Reg.exe add "HKC
我想为 ffplay 或 ffmpeg 传递多个标题,它说我需要用 CRLF 拆分。在 linux 上我可以使用 \或 $'\r\n'但是 window 怎么样? SET CRLF=^ ffplay
我是 3D 软件 Blender 的用户。 我正在尝试在 CMD 中运行,因为 Blender 提供了 CLI 控制。 下面的代码工作正常。 blender -b "my.blend" ^ --pyt
我有一个包含版本资源的文件,其中填充了文件版本/产品版本字段。我需要通过 BAT 文件检索产品版本。例如,我在 bat 文件的输出中有 ProductVersion 1.0.1 的文件我不想有字符串“
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 去年关闭。 Improve this
我可以运行 forfiles 命令与cmd /c ,正如预期的那样 C:\>forfiles/c "cmd/c ping/a" 必须指定 IP 地址。 但是,如果我删除 cmd /c ,它不再识别任何
我有一个函数,它可以获取所有 USB 连接设备的信息。 connected_devices = :os.cmd('usb-devices | grep -A 1 Product=') 当我使用 :os
我是一名优秀的程序员,十分优秀!