1

I'm writing a batch script that will check the MD5sum of all files in a folder and i found this script on this site

for /r %%f in (*) do (certutil -hashfile "%%f" MD5) >> output.txt

This one is working but any idea how can i get the hash only, without the other text

Please see text highlighted on this image

Thanks!

Squashman
  • 13,649
  • 5
  • 27
  • 36
Sonel Kim
  • 17
  • 4

2 Answers2

2

Since the two lines that you don't want to include both contain the string hash somewhere in them, you can use find to filter those lines out.

for /r %%f in (*) do (certutil -hashfile "%%f" MD5 | find /v "hash") >> output.txt

find /v says "return all lines that do not contain this string."

SomethingDark
  • 13,229
  • 5
  • 50
  • 55
  • There's a simpler way of using `find.exe`, the character set used for the MD5 result is effectively alphanumeric, so if you take a look at the output, the lines requested for omission all contain a matching character. That character is the colon **`:`**, so you would use `find /v ":"`. _(As a side note, although unlikely, I'm not sure that the string of four alphanumeric characters `hash` could never be returned in the MD5 result)_. – Compo May 06 '21 at 09:48
  • MD5s are hexadecimal characters, so `h` will never appear in the output. I could save 3 characters and just do `find /v "h"` though. – SomethingDark May 06 '21 at 15:19
0

DOS is hard to work with (compared to bash on linux, for example), but I managed to get something like this working locally:

Simple example using just : as a deliminator (like in your example, where the MD5 hash is after the :):

$ @echo hello:me
hello:me
$ for /f "tokens=1,2 delims==:" %a in ('echo hello:me') do @echo %b
me
  • tokens=1,2: fetch tokens 1 & 2 - assign to %a %b respectively
  • delims==:: tokenize on :
  • echo %b: print token #2

Extrapolate to your case (which I can't reproduce locally, so this is a guess):

$ for /f "tokens=1,2 delims==:" %a in ('for /r %%f in (*) do (certutil -hashfile "%%f" MD5)') do @echo %b

See if that gets you closer.

Jevon Kendon
  • 545
  • 4
  • 13