Batch file CertUtil-哈希文件输出到文件和错误处理

Batch file CertUtil-哈希文件输出到文件和错误处理,batch-file,certutil,Batch File,Certutil,我需要从同一批处理文件中生成的文件列表中生成一个带有hashfile标记的文件。以下是我目前掌握的代码: @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

我需要从同一批处理文件中生成的文件列表中生成一个带有hashfile标记的文件。以下是我目前掌握的代码:

@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
对于我为其生成哈希文件的大多数文件,我得到如下结果:

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.
但有时会出现如下错误:

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.
  • 为什么有些文件会出现此错误
  • 如何删除以CertUtil开头的任何行:这样我就没有多余的行,或者有没有办法只将CertUtil命令的前两行写入文件
  • %hashfile%
    以其最终形式出现后,我想运行
    certutil-hashfile”%hashfile%MD5
    ,并仅将哈希代码分配给一个变量。其语法是什么

  • 稍微修改的代码片段显示
    0x800703ee(WIN32:1006错误\u文件\u无效)
    仅针对零长度文件的错误消息(参见
    rem
    注释)


    有关
    %%~zG
    “%%~G”
    说明,请参见。

    稍微修改的代码片段显示
    0x800703ee(WIN32:1006错误文件无效)
    仅针对零长度文件的错误消息(参见
    rem
    注释)


    有关
    %%~zG
    “%%~G”
    的说明,请参阅。

    IMO批次中的计数步骤更容易通过查找/v/N完成

    但是,由于每次批处理运行时都会重新创建列表和哈希文件,因此根本不需要中间步骤。
    此堆叠for-if-for将在一次运行中完成:

    @echo off
    setlocal enabledelayedexpansion
    
    :: Set the variables for this script.
    set testfolder=c:\test\test folder
    set hashfile=c:\test\output\md5-hashes.csv
    
    >%hashfile% (
      echo "File","MD5-hash"
      For /f "delims=" %%A in (
        'dir /s /b /a-d "%testfolder%"'
      ) Do If %%~sA Gtr 0 For /f "delims=" %%B in (
        'certutil -hashfile %%A MD5 ^|findstr /i ^[0-9a-f][0-9a-f].[0-9a-f][0-9a-f].[0-9a-f][0-9a-f]'
      ) Do Echo:"%%~A","%%~B"
    )
    
    此部分输出碰巧引用了可能更适合此任务的工具:-)


    在我看来,批处理中的计数步骤更容易通过查找/v/N“”完成

    但是,由于每次批处理运行时都会重新创建列表和哈希文件,因此根本不需要中间步骤。
    此堆叠for-if-for将在一次运行中完成:

    @echo off
    setlocal enabledelayedexpansion
    
    :: Set the variables for this script.
    set testfolder=c:\test\test folder
    set hashfile=c:\test\output\md5-hashes.csv
    
    >%hashfile% (
      echo "File","MD5-hash"
      For /f "delims=" %%A in (
        'dir /s /b /a-d "%testfolder%"'
      ) Do If %%~sA Gtr 0 For /f "delims=" %%B in (
        'certutil -hashfile %%A MD5 ^|findstr /i ^[0-9a-f][0-9a-f].[0-9a-f][0-9a-f].[0-9a-f][0-9a-f]'
      ) Do Echo:"%%~A","%%~B"
    )
    
    此部分输出碰巧引用了可能更适合此任务的工具:-)

    @echo off
    setlocal enabledelayedexpansion
    
    :: Set the variables for this script.
    set testfolder=c:\test\test folder
    set hashfile=c:\test\output\md5-hashes.csv
    
    >%hashfile% (
      echo "File","MD5-hash"
      For /f "delims=" %%A in (
        'dir /s /b /a-d "%testfolder%"'
      ) Do If %%~sA Gtr 0 For /f "delims=" %%B in (
        'certutil -hashfile %%A MD5 ^|findstr /i ^[0-9a-f][0-9a-f].[0-9a-f][0-9a-f].[0-9a-f][0-9a-f]'
      ) Do Echo:"%%~A","%%~B"
    )
    
    > type md5-hashes.csv
    "File","MD5-hash"
    "c:\test\md5\hashdeep.exe","23 03 ea 53 52 03 c7 93 05 49 0d 6c 20 be 84 54"
    "c:\test\md5\hashdeep64.exe","66 5c cc 57 a8 4c 56 39 c4 e5 15 16 86 cc 04 32"
    "c:\test\md5\md5deep.exe","23 03 ea 53 52 03 c7 93 05 49 0d 6c 20 be 84 54"
    "c:\test\md5\md5deep64.exe","66 5c cc 57 a8 4c 56 39 c4 e5 15 16 86 cc 04 32"