我有一个包含文件的目录和一个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的了解是有限的。如有任何代码建议,将不胜感激。
发布于 2022-10-21 06:15:26
@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 :EOFdir options /b只显示名称(不显示大小、日期等),/s将扫描子目录,并生成具有完整路径的文件名,而/a-d则取消目录名。u:\j*是清单的起始位置;驱动u:,所有开始j的文件(用于测试)。
pushd命令使指定目录当前,因此修改后的dir命令将只扫描该目录中的所有文件名,而不扫描目录名(因为没有提供起始目录和文件)。
popd命令返回到原始目录。
https://stackoverflow.com/questions/74148620
复制相似问题