12

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
  • 532
  • 1
  • 9
  • 23

7 Answers7

20

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"
thor
  • 21,418
  • 31
  • 87
  • 173
MonkeyK
  • 209
  • 2
  • 2
  • How do I get this in a variable? Like I want the sha hash to be assigned to a variable like "hash", and then use the variable %hash% where I want to? – batchcoding____s Dec 15 '21 at 14:43
4

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
  • 22,678
  • 7
  • 27
  • 43
elzooilogico
  • 1,659
  • 2
  • 17
  • 18
  • 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
4

Use powershell command:

$(CertUtil -hashfile C:\TEMP\MyDataFile.img MD5)[1] -replace " ",""
john
  • 41
  • 1
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
  • 55,367
  • 18
  • 148
  • 187
  • 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
0

The MD5.bat works brilliantly. I would just add the following to the top part.

set filesize=%~z1

if %filesize% lss 1 (

if "%~2" neq "" (
    endlocal && (
        set "%~2=0"
    ) 
) else (
    echo 0  
)
goto :eof

)

certUtil does not play nice with empty files, and returns an error. Checking the filesize for empty files allow you to return a zero in this case, or any string you want when the file is empty instead.

0

I use the following checksum.bat file:

@echo off
setlocal

REM Wrapper over built-in Windows utility `certutil` that removes spaces

set FILE_NAME=%~1
set CHECK_SUM=%~2

set RESULT=
for /f "delims=" %%s in ('certutil -hashfile "%FILE_NAME%" %CHECK_SUM% ^| find /i /v "%CHECK_SUM%" ^| find /i /v "certutil"') do (
    call :remove_spaces "%%s"
)

echo %RESULT%

exit /b

:remove_spaces
set SUM=%~1
set RESULT=%SUM: =%
exit /b

Usage example:

>checksum.bat some_file.zip SHA256
debea8cf24b93b6c34ec06bd4d85f4bc3b710d596419acbce25928e88e2805d0
Nick Legend
  • 789
  • 1
  • 7
  • 21
0

Simple filter-out any lines contain other than alphanumeric characters

CertUtil -hashfile "path_to_file" MD5 | findstr /R "[0-9][a-z]"
user2956477
  • 1,208
  • 9
  • 17