我需要从文件中读取前n个字节,以确保文件确实是Word文件(.docx)(忽略扩展名)。
我该怎么做?
答案 0 :(得分:1)
通过 fsutil
文件截断读取第 1 个字节(先复制原始文件):
FSUTIL file seteof <file> <n bytes>
或者只是 fc /b
您所追求的前几个字节:
set n=3
set file=test.one
fsutil file createnew A %n%
fc /b A %file% > B & del A
set /a n=%n%-1 >Nul
for /l %j in (0,1,%n%) do cmd /c exit /b %j & set hex=!=exitcode:~-2!& (find "!hex!: "<B || echo doh: la 00)>> A
(for /f "tokens=3" %i in (A) do set /p=%i <nul) & del A & del B
注意:
使用 makecab
和内置 cmd
命令从代码页 437 生成 binary/null characters,或者 fsutil
空字节。
更新:
这是获取文件前 3 个字节的 VBatch 混合代码:
@echo off
echo Set fso=CreateObject("Scripting.FileSystemObject") > some.vbs
echo Set f=fso.OpenTextFile("%~1"):buf=f.Read(3):f.Close >> some.vbs
REM OpenTextFile opens any file as a binary stream; f.read(n) reads the first n bytes of that stream
echo wscript.echo Hex(ASCb(mid(buf,1,1))) ^& "," ^& Hex(ASCb(mid(buf,2,1))) ^& "," ^& Hex(ASCb(mid(buf,3,1))) >> some.vbs
for /f "tokens=1-3 delims=," %%A in ('cscript //nologo some.vbs') do (
set byte1=0%%A
set byte2=0%%B
set byte3=0%%C
)
echo %byte1:~-2% %byte2:~-2% %byte3:~-2%
在 Win 10 CMD 中测试