gpt4 book ai didi

cmd - 如何使用命令行知道文件的编码?

转载 作者:行者123 更新时间:2023-12-05 04:11:14 26 4
gpt4 key购买 nike

有什么命令可以知道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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com