1

Simply put I am trying to take a hash of all of my files in a given directory. I am doing this by calling CertUtil and running:

for %F in (L:\TestDirectory\*) 
do (certutil -hashfile "%F" MD5&echo.) >> L:\certutilOutput.txt

This works well, but only for the current directory as it does not go into my next subfolder: "L:\TestDirectory\NetFolder\ which contains another set of files. I would like this to be able to go down several layers.

I feel like I am missing something very simple, any help is appreciated.

TheGoblinPopper
  • 149
  • 1
  • 3
  • 12
  • 2
    Help on the `for` command in CMD is obtained by typing `for /?` and the third chunk is about the `/r` option which does exactly what you want. – dave_thompson_085 Sep 29 '18 at 05:34
  • When I used /r it pauses for 3-4 seconds and then finishes having printed nothing. If I change the directory from what is there at all and use /r it jumps to my C:\ and begins running my current directory that the command is launched on rather than the one specified. Not sure how to get around this, but I was playing wit the /r. – TheGoblinPopper Sep 30 '18 at 01:19
  • 1
    I'm not clear quite what you did, but the 'set' must be relative to the tested directories: `for /r L:\testdirectory %f in (*) do ( ... command(s) using %f ...)` – dave_thompson_085 Oct 03 '18 at 00:10
  • Yep... that was it. As I said, I knew it was something simple out of place. If you would like to get credit for the answer post that comment as an Anwer and Ill accept it. – TheGoblinPopper Oct 03 '18 at 21:32

2 Answers2

0

To base this on the original code and put it in the full solution that @dave_thompson_085 referenced, save the following as a bat file:

@echo off
for /R "L:\TestDirectory" %%f in (*) do ( 
  certutil -hashfile "%%f" MD5
)>>L:\certutilOutput.txt
Doug
  • 6,446
  • 9
  • 74
  • 107
0

Brilliant minds work alike. What are the odds that, after 3+ years of sitting unanswered, two of us would be working on answers to this question at the same time?

My variation features (1) the optional use of FIND to eliminate potentially unwanted clutter from the output and (2) a check to remove a preexisting output file:

@echo off
if exist "L:\certutilOutput.txt" del "L:\certutilOutput.txt"
for /r "L:\TestDirectory" %%F in (*) do certutil -hashfile "%%F" MD5 | find /i /v "certutil:" >> "L:\certutilOutput.txt"

Sadly, it still doesn't put both the filename and the hash on one line, which is what I've been seeking.

Ray Woodcock
  • 209
  • 3
  • 13