I need to generate a file with hashfile tags from a list of files that is generated within the same batch file. Here is the code that I have so far:
@echo off
setlocal enabledelayedexpansion
:: Set the variables for this script.
set testfolder=c:\test\test folder
set listfile=c:\test\output\file list.txt
set hashfile=c:\test\output\hashes.txt
:: Delete any of the files that were created the last time this script was ran.
del "%hashfile%"
del "%listfile%"
cls
:: Generate a file with a list of all of the files (with path) in designated folder and subdirectories.
:: Directory and subdirectory names are not included in the list. Only files.
dir /s /b /a-d "%testfolder%" > "%listfile%"
:: Assign each line of the file above to its own variable.
set counter=1
for /f "usebackq delims=" %%x in ("%listfile%") do (
set "line_!counter!=%%x"
set /a counter+=1
)
:: Count the number of lines in the above file to use as a reference point.
set /a numlines=counter - 1
:: Generate an MD5 hash for each variable and write it to a file with a blank space between each.
for /l %%x in (1,1,%numlines%) do (
certutil -hashfile "!line_%%x!" MD5 >> "%hashfile%"
echo( >> "%hashfile%"
)
eof
For most of the files that I generate a hashfile for, I get something like:
MD5 hash of file c:\test\test folder\Citrix 2.bmp:
31 34 d6 04 cd b0 4b ef a7 63 c3 e9 ae a8 3d 01
CertUtil: -hashfile command completed successfully.
But there are times where I get an error like:
CertUtil: -hashfile command FAILED: 0x800703ee (WIN32: 1006)
CertUtil: The volume for a file has been externally altered so that the opened file is no longer valid.
- Why would some files be giving this error?
- How can I delete any line that begins with CertUtil: so I don't have unnecessary lines or is there a way to only write the first 2 lines of the CertUtil command to the file.
- After the
%hashfile%
is in its final form, I want to runcertutil -hashfile "%hashfile% MD5
and assign just the hash code to a variable. What is the syntax for that?