2

我有这个批处理代码,但它是错误的,我需要在屏幕上查看文件夹 2 中不在文件夹 1 中的文件的名称,并将它们与您的 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") 
)
4

2 回答 2

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.
于 2017-08-04T00:59:47.197 回答
3
  • certutil hash 没有其他行的冒号。
  • 如果没有相应的文件,只有散列的文件没有意义
  • 以下批次为两个文件夹创建哈希文件名对
  • 此外,检查 folder2 中的每个散列是否存在于 folder1 - 如果不存在,则将其回显到屏幕。

@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

示例输出_.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
于 2017-08-03T22:04:32.170 回答