我有一个包含文件的目录和一个ControlFile.txt,其中包含各种文件的SHA256和的列表。我正试图想出一个批处理过程来循环浏览目录中的文件,计算每个文件的SHA256值,然后比较计算出的SHA256是否存在于ControlFile.txt中,并相应地进行分支。
我试图用以下内容制作一个工作脚本,但我认为我缺少了一些关键元素:
for /R . %%f in (*.*) do (
find /c "(certutil -hashfile "%%f" SHA256 | findstr /V "hash")" ControlFile.txt > NUL
if %errorlevel% equ 1 goto notfound
echo "%%f" found
goto done
:notfound
echo "%%f" notfound
goto done
:done)
我相信我可能需要为给定的SHA256值设置一个变量,并在循环中使用它来产生我试图实现的比较函数,但我对批处理文件和cmd的了解有限。任何代码建议都将不胜感激。
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:your files"
SET "filename1=%sourcedir%q74148620.txt"
FOR /f "delims=" %%b IN ('dir /s /b /a-d "u:j*" ') DO (
FOR /f %%y IN ('certutil -hashfile "%%b" SHA256 ^| find /V ":"') do (
findstr /x "%%y" "%filename1%" > NUL
IF ERRORLEVEL 1 (
ECHO "%%b" NOT found
) ELSE (
ECHO "%%b" found
)
)
)
GOTO :EOF
我使用了一个j*
的文件掩码进行测试-更改以适应。
只需依次对每个文件运行certutil
例程,过滤掉任何包含:
的行,留下SHA256数据。将该值定位为/x
,精确到SHA256值文件中的一行。如果找到匹配,则将errorlevel
设置为0
,否则设置为非0
,然后打开errorlevel
。
===对的小修订不包括子目录===
@ECHO OFF
SETLOCAL
rem The following settings for the source directory and filename are names
rem that I use for testing and deliberately include names which include spaces to make sure
rem that the process works using such names. These will need to be changed to suit your situation.
SET "sourcedir=u:your files"
SET "filename1=%sourcedir%q74148620.txt"
PUSHD "%sourcedir%"
FOR /f "delims=" %%b IN ('dir /b /a-d') DO (
FOR /f %%y IN ('certutil -hashfile "%%b" SHA256 ^| find /V ":"') do (
findstr /x "%%y" "%filename1%" > NUL
IF ERRORLEVEL 1 (
ECHO "%%b" NOT found
) ELSE (
ECHO "%%b" found
)
)
)
POPD
GOTO :EOF
dir
选项/b
仅显示名称(而不是大小、日期等(,/s
将扫描子目录并生成带有完整路径的文件名,而/a-d
则不显示目录名。u:j*
是列表的开始位置;驱动器u:
,所有以j
开头的文件(用于测试(。
pushd
命令使指定的目录成为当前目录,因此修订后的dir
命令将只扫描该目录中的所有文件名,而不扫描目录名(因为没有提供起始目录和文件掩码(。
popd
命令返回到原始目录。