8

I want to save a result of

CertUtil -hashfile "path_to_file" MD5

to a variable and remove spaces of the hash in command line command (to be more particular, I wan to use this in Command Line of post-processing in VS 2015 C++).

Currently the result is as follows:

1) C:\Users\admin>CertUtil -hashfile ping.txt MD5
2) MD5 hash of file ping.txt:
3) 4f 75 c2 2c 20 b4 81 54 72 2c a5 7c 95 7a 66 88
4) CertUtil: -hashfile command completed successfully.

I just need the line 3) - save the hexa string to a variable and then remove the spaces. Thanks a lot!

Dom
  • 464
  • 1
  • 7
  • 20

4 Answers4

15

I am totally late to this, but how about just using find to get rid of the unwanted lines:

 CertUtil -hashfile "path_to_file" MD5 | find /i /v "md5" | find /i /v "certutil"
tinlyx
  • 18,900
  • 26
  • 81
  • 148
MonkeyK
  • 151
  • 1
  • 2
3

you can with this ready to use MD5.bat:

call MD5.bat  "path_to_file" md5_var
echo %md5_var%

if don't want a whole new separate script you can just use the last for loop from the link.

npocmaka
  • 51,748
  • 17
  • 123
  • 166
  • 1
    Thanks for the link, this works for me: `@echo off setlocal enableDelayedExpansion set "md5=" for /f "skip=1 tokens=* delims=" %%# in ('certutil -hashfile "ping.txt" MD5') do (if not defined md5 (for %%Z in (%%#) do set "md5=!md5!%%Z")) echo %md5% endlocal` – Dom Aug 04 '16 at 16:05
  • 1
    I would vote as accepted answer if you extract the desired code along with the link... your bat file is cool, I just need the part without any checks. However, thanks! :-) – Dom Aug 04 '16 at 20:13
3

Despite the answer above, typicall setlocal enabledelayedexpansion issue

@echo off
setlocal enabledelayedexpansion
set /a count=1 
for /f "skip=1 delims=:" %%a in ('CertUtil -hashfile "ping.txt" MD5') do (
  if !count! equ 1 set "md5=%%a"
  set/a count+=1
)
set "md5=%md5: =%
echo %md5%
endlocal
exit/B
Gerhard
  • 18,114
  • 5
  • 20
  • 38
elzooilogico
  • 1,623
  • 2
  • 13
  • 16
  • my script has no problems with delayed expansion.What do you mean? – npocmaka Aug 04 '16 at 16:09
  • @npockmaka Your script is ok, I was thinking of an straight solution. Nothing to do with you, but with the OP question. – elzooilogico Aug 04 '16 at 16:14
  • @npockmaka I apologize, I haven't read the last line of your post. _if don't want a whole new separate script you can just use the last for loop from the link_. Cannot delete post. – elzooilogico Aug 04 '16 at 16:27
3

Use powershell command:

$(CertUtil -hashfile C:\TEMP\MyDataFile.img MD5)[1] -replace " ",""
john
  • 31
  • 1