使用BATCH中的HASH SHA1比较两个文件夹中的文件

时间:2017-08-03 20:58:20

标签: batch-file hash certutil

我有这个批处理代码,但是错了,我需要的是在屏幕上看到folder2中不在folder1中的文件的名称,将它们与你的HASH SHA1进行比较。临时文件位于同一目录中。 感谢您的评论

@echo off
cd folder1
FOR /F "Delims=" %%A in ('DIR /B/A-D *.*') DO (
    certUtil -hashfile "%%A" SHA1 | findstr /VI "HASH"| findstr /VI "certutil"
) >>folder2\output.tmp

cd folder2
FOR /F "Delims=" %%B in ('DIR /B/A-D *.* ^|Findstr /VEIL ".tmp"') DO (
    certUtil -hashfile "%%B" SHA1 | findstr /VI "HASH"| findstr /VI "certutil" >>output2.tmp
    FOR /F "Delims=" %%C in ('TYPE output2.tmp^|findstr /XLIV /G:output.tmp') DO (echo "%%B") 
)

2 个答案:

答案 0 :(得分:4)

这是另一个不使用临时文件的解决方案。相反,它会创建一种像f|filename.ext=hash这样的关联数组。所有哈希都存储为变量,并且可以使用set "f|"枚举一系列变量。基本上,这可以很容易地获得与目录1中的文件名绑定的哈希列表,然后使用此列表来比较目录2.如果预期变量未定义,则目录2中的文件不存在于目录1中。如果它定义,然后比较哈希并取消定义它。无论在末尾定义什么,都表示目录1中存在文件,但目录2中不存在。

@echo off & setlocal

if "%~2"=="" goto usage

pushd "%~1" || goto usage
for /f "delims=" %%I in ('dir /b /a:-d') do (
    for /f %%x in ('certutil -hashfile "%%~I" ^| find /v ":"') do set "f|%%~nxI=%%~x"
)
popd

pushd "%~2" || goto usage
for /f "delims=" %%I in ('dir /b /a:-d') do (
    if not defined f^|%%~nxI (
        echo %%~nxI does not exist in %~nx1\
    ) else (
        for /f %%x in ('certutil -hashfile "%%~I" ^| find /v ":"') do (
            setlocal enabledelayedexpansion
            if not "%%~x"=="!f|%%~nxI!" (
                echo %%~nxI hash mismatch
            )
            endlocal
            set "f|%%~nxI="
        )
    )
)

for /f "tokens=2 delims=|=" %%I in ('cmd /c set "f|" 2^>NUL') do (
    echo %%I does not exist in %~nx2\
)

goto :EOF

:usage
echo Usage: %~nx0 dir1 dir2
echo compares files in dir1 with those in dir2.

答案 1 :(得分:3)

  • certutil hash没有其他行的冒号。
  • 只有哈希的文件没有相应的文件
  • 是没有意义的
  • 以下批处理为两个文件夹
  • 创建哈希文件名对
  • 另外,如果文件夹1中存在,则检查来自folder2的每个哈希 - 如果不存在则回显到屏幕。
@echo off
Set Dir1=A:\
Set Dir2=Q:\Test\2017\08\03\

PushD "%Dir1%"
(FOR /F "Delims=" %%A in ('DIR /B/A-D *.*'
 ) DO For /f "delims=" %%B in (
 'certUtil -hashfile "%%A" SHA1 ^| findstr /V ":"'
 ) Do Echo %%B %%~fA
)> "%Dir2%\output1.tmp"
PopD

PushD "%Dir2%"
Type Nul >output2.tmp
FOR /F "Delims=" %%A in ('DIR /B/A-D *.* ^|Findstr /LIVE ".tmp"'
) DO For /f "delims=" %%B in ('certUtil -hashfile "%%A" SHA1 ^| findstr /V ":"') Do (
       >> output2.tmp Echo %%B %%~fA
       Findstr "%%B" output1.tmp >Nul 2>&1 || Echo Hash %%B not in "%Dir1%" File %%~fA 
     )
)
PopD

示例运行:

> Q:\Test\2017\08\03\SO_45494397.cmd
Hash fcfd29ab1ba8b64411d5ce461a35f07907862533 not in "A:\" File Q:\Test\2017\08\03\Get-EpubMetaInfo.ps1
Hash aa37d47dc96380532c88559045b6c3fa080e2556 not in "A:\" File Q:\Test\2017\08\03\Get-MSebooks.ps1
Hash ae29aeca5a433993ec854ddea6d8469516d2293c not in "A:\" File Q:\Test\2017\08\03\Handle-ZipFile.psm1
Hash 2d0d7fc7927f007b8aba4032d1c9fe86074ec8a1 not in "A:\" File Q:\Test\2017\08\03\SO_45494397.cmd

示例output_.tmp

> Type output1.tmp
c10937240668c7c09dbac247b5cb0e30f027cfe6 A:\SO_45490060.cmd
47c005b12889d32107b53bdbd16e94f029d330c4 A:\SO_45491838.cmd
af6cccbeec7b80cbb37143316bd910bf6dcf622e A:\SO_45494397.cmd

> Type output2.tmp
fcfd29ab1ba8b64411d5ce461a35f07907862533 Q:\Test\2017\08\03\Get-EpubMetaInfo.ps1
aa37d47dc96380532c88559045b6c3fa080e2556 Q:\Test\2017\08\03\Get-MSebooks.ps1
ae29aeca5a433993ec854ddea6d8469516d2293c Q:\Test\2017\08\03\Handle-ZipFile.psm1
c10937240668c7c09dbac247b5cb0e30f027cfe6 Q:\Test\2017\08\03\SO_45490060.cmd
47c005b12889d32107b53bdbd16e94f029d330c4 Q:\Test\2017\08\03\SO_45491838.cmd
52b8e933411859e450fde3e8735658d9f52159b0 Q:\Test\2017\08\03\SO_45494397.cmd