验证文件的 md5

Validate md5 of file

我想制作一个批处理文件:

  1. 将 md5 设置为变量“temp”
  2. 获取新文件的md5并将其设置为“newmd5”
  3. 比较 temp 和 newmd5
  4. 如果相等则回显“ok”。否则回显“错误的文件”

这是我到目前为止写的:

@ECHO OFF
set temp=202cb962ac59075b964b07152d234b70
CertUtil -hashfile test123.txt MD5
PAUSE

这是结果:

MD5 hash of test123.txt:
202cb962ac59075b964b07152d234b70

CertUtil: -hashfile command completed successfully.

Press any key to continue..

我坚持在“newmd5”变量中设置输出 md5 并将其与温度进行比较。

所以您的问题可以简化为“如何将 certutil 给出的 MD5 放入变量中?”

这可以通过 for /f 循环来完成:

set "test="
for /f "skip=1 delims=" %%a in ('certutil -hashfile test123.txt MD5') do if not defined test set "test=%%a"
set "test=%test: =%"

"skip=1" 将跳过第一行 (MD5 has of ...),if not defined 只考虑第二行(散列),忽略第三行 (CertUtil: -hashfile command completed successfully.)

我想您不需要使用 if 命令来比较两个变量。