3
\$\begingroup\$

... isn't easy.

@echo off&(for /f "tokens=*" %%a in ('certutil -hashfile "%1" MD5') do echo %%a&for /f "delims=" %%b in ('echo %%a^|findstr /r /i /x [0-9A-F]*') do if "%%b" neq "" echo %%b|clip&pause&exit)&pause

PowerShell is slow to load so I used batch instead. I use this command to extend the context menu of windows explorer, hence a single line.

There is one minor issue; certutil doesn't support empty files.

Bonus registry entries: https://paste.ee/p/sgSJY

asked Oct 29, 2017 at 17:28
\$\endgroup\$
1
  • \$\begingroup\$ Why not put in multiple lines? Programs associated with file types can be arbitrarily long, as long as the invocation is single line. \$\endgroup\$ Commented Feb 21, 2018 at 18:27

1 Answer 1

2
\$\begingroup\$

All zero-sized files will have the same checksum using the same given hash algorithm. Hence, use the following batch-script:

@ECHO OFF
SETLOCAL EnableExtensions
for /f %%G in ("%~1") do set "_len=%%~zG"
if %_len% EQU 0 (
 echo MD5 hash of %1
 echo D41D8CD98F00B204E9800998ECF8427E
 echo CertUtil bypassed for zero file size
 echo D41D8CD98F00B204E9800998ECF8427E|clip 
) else (
 for /f "tokens=*" %%a in ('certutil -hashfile "%~1" MD5') do (
 echo %%a
 for /f "delims=" %%b in ('echo %%a^|findstr /r /i /x ^[0-9A-F]*[0-9A-F]$') do (
 if "%%b" neq "" echo %%b|clip
 )
 )
)
pause

Note that %%~zG expands %%G to size of file (read call /?).

if "%%b" neq "" echo %%b|clip seems to be superfluous then. Simple echo %%b|clip should suffice.

Sample output:

==> D:\bat\CodeReview179105円.bat out.txt
MD5 hash of out.txt
D41D8CD98F00B204E9800998ECF8427E
CertUtil bypassed for zero file size
Press any key to continue . . .
==> D:\bat\CodeReview179105円.bat D:\bat\CodeReview179105円.bat
MD5 hash of D:\bat\CodeReview179105円.bat:
c2a4cabffac79a26ee5ed7c97cefb44b
CertUtil: -hashfile command completed successfully.
Press any key to continue . . .
==>
answered Dec 13, 2018 at 22:02
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.