certutil -hashfile of a directory

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
docdave78
Posts: 3
Joined: 10 Oct 2019 09:22

certutil -hashfile of a directory

#1 Post by docdave78 » 10 Oct 2019 09:41

I'm trying to run certutil on a directory to output the results to a file using a wildcard to identify the files.

the directory is c:/temp/mfiles
the file extensions are .fdf
the hashfile are SHA256

I'm familiar with using the certutil for a single file, but I'm not sure how to do it for a directory, I have 250+ files that I need to run this on. I've tried multiple commands and tried using FOR, but I'm not well versed in dos to get the results.

The command I use for a single file is, certutil -hashfile file1.fdf SHA256>filehash256.txt, which returns the result I need, but I don't know how to employ it for the contents of a directory.

I am unable to download other utilities do to IT restrictions on my computer so this has to be done with built in commands.

Thank you

aGerman
Expert
Posts: 3753
Joined: 22 Jan 2010 18:01
Location: Germany

Re: certutil -hashfile of a directory

#2 Post by aGerman » 10 Oct 2019 10:12

I guess it should be either

Code: Select all

for %%i in ("C:\temp\mfiles\*.fdf") do certutil -hashfile "%%~i" SHA256| >"%%~dpni-hash256.txt" findstr /iv "hash certutil"
or

Code: Select all

>"C:\temp\mfiles\filehash256.txt" (for %%i in ("C:\temp\mfiles\*.fdf") do certutil -hashfile "%%~i" SHA256|findstr /iv "certutil")
depending on whether or not you want to have the hashes in separate files.

Steffen

docdave78
Posts: 3
Joined: 10 Oct 2019 09:22

Re: certutil -hashfile of a directory

#3 Post by docdave78 » 10 Oct 2019 12:19

Steffen,

Thank you, I've copied the command line into my cmd window exactly as you've provided and its giving me an error of:

"%%i was unexpected at this time.

I should have noted that I'm running Windows 10 Enterprise.

aGerman
Expert
Posts: 3753
Joined: 22 Jan 2010 18:01
Location: Germany

Re: certutil -hashfile of a directory

#4 Post by aGerman » 10 Oct 2019 12:25

The code was made for a Batch script. In a cmd window things work differently. Use %i rather than %%i and after DO use @certutil rather than certutil.

Steffen

dbenham
Expert
Posts: 2288
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: certutil -hashfile of a directory

#5 Post by dbenham » 10 Oct 2019 12:27

aGerman gave you code to put in a batch script.
If you run from the command prompt then each %% must be modified to a single %.

You might want to checkout HASHSUM.BAT - It is a batch script that only uses native commands.

The command to generate the hash values for all the files would be:

Code: Select all

hashsum /p c:/temp/mfiles  *.fdf >filehash256.txt
The output would look something like:

Code: Select all

d36007cbb2e33d620234e5c5b3fbfc49244bc56bb4fd7acd32e2365c3fcd512b *file1.fdf
ffa95d3f56558241b6326aa69f9c77079a35a8f9814e5a5d6680ea655ae0ad7c *file2.fdf
ea645e4c2fb343bbc49e27651ab4554f21d4bde6ac3781b6cc24f8a8cc34acbe *file3.fdf
etc.

Dave Benham

docdave78
Posts: 3
Joined: 10 Oct 2019 09:22

Re: certutil -hashfile of a directory

#6 Post by docdave78 » 10 Oct 2019 14:27

Steffen, Dave,

Thanks for your help. I was able to get it to work with the hashsum.bat link.

Dave

Post Reply