Izhar Mishli
Izhar Mishli

Reputation: 31

Validate md5 of file

I want to make a batch file that:

  1. Set a md5 to variable “temp”
  2. Get md5 of a new file and set it to “newmd5”
  3. Compare temp to newmd5
  4. Echo “ok” if equal. Else echo “wrong file”

This is what I wrote so far:

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

This is the result:

MD5 hash of test123.txt:
202cb962ac59075b964b07152d234b70

CertUtil: -hashfile command completed successfully.

Press any key to continue..

I'm stuck with set the output md5 in “newmd5” variable and compare it to temp.

Upvotes: 1

Views: 590

Answers (1)

Stephan
Stephan

Reputation: 56228

So your question can be reduced to "how to put the MD5 given by certutil to a variable?"

That can be done by a for /f loop:

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" will skip the first line (MD5 has of ...), if not defined takes care that only the second line (the hash) is considered, ignoring the third line (CertUtil: -hashfile command completed successfully.)

I guess you don't need help with the if command to compare the two variables.

Upvotes: 1

Related Questions