在BATCH中使用HASH SHA1比较两个文件夹中的文件 [英] Compare files from two folders using HASH SHA1 in BATCH

查看:306
本文介绍了在BATCH中使用HASH SHA1比较两个文件夹中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个批处理代码,但它是错误的,我需要的是在屏幕上查看文件夹2中不在folder1中的文件的名称,并将它们与您的HASH SHA1进行比较。临时文件位于相同的目录中。
我感谢您的意见

I have this batch code but it is wrong, what I need is to see on screen the name of the files in folder2 that are not in folder1 comparing them with your HASH SHA1. Temporary files are in the same directory. I appreciate your comments

@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") 
)


推荐答案

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

Here's another solution that uses no temporary files. Instead, it creates a sort of associative array like f|filename.ext=hash. All the hashes are stored as variables, and the series of variables can be enumerated with set "f|". Basically, this makes it easy to get a list of hashes tied to filenames in directory 1, then use this list to compare directory 2. If an expected variable is undefined, then the file in directory 2 does not exist in directory 1. If it is defined, then compare the hashes and undefine it. Whatever is left defined at the end indicates a file exists in directory 1 but not in directory 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.

这篇关于在BATCH中使用HASH SHA1比较两个文件夹中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆